Serilog to capture and log Events / Exceptions Automatically - serilog

Can Serilog capture and Log events (or) exceptions automatically with out writing the log statements.

Related

How to return the ID of the log when logging with Microsoft.Extensions.Logging

In my .NET 6 web application I'm using the following infrastructure to log various events and exceptions:
Microsoft.Extensions.Logging as logging API
Serilog as logging framework
MS SQL database as loging sink (i.e. the target where the logs are going to be written).
In the API layer of the web application there's a middleware that catches all exceptions, logs them and returns the exception message as JSON string to the client.
Together with the exception message I'd like to return also the ID of the created log-entry to the client. Unfortunately, the methods of Microsoft.Extensions.Logging's LoggerExtension class (e.g. LogError()) don't return any value. Is there any way to obtain the ID of the created database record?

Attach and detach listeners to Serilog on the fly?

Is it possible to add or remove listeners / sinks dynamically using Serilog? I see from the configuration documentation that sinks are determined in the source code by calling WriteTo. I suppose I can create a section in my application startup to determine whether to register a sink or not. But is there something similar to the way ETW publish and subscribe events? For example we can run Perfview to listen to ETW events on the fly.

Log failover with Serilog

Is it possible, using Serilog, to log to a webservice of mine and if throws an error (no internet, for instance) to log to a RollingFile.
Should only log to RollingFile if WebService fails.
You can implement this yourself by creating a custom sink that wraps another new RollingFileSink(...) and only forwards events if the web service call fails.
To do this you'd implement ILogEventSink or, if the web service accepts batches, create a subclass of PeriodicBatchingSink.

Does Elmah run into race condition when 2 session write to the same log file simultaneously?

I am developing with ASP.NET MVC, and I am considering using Elmah.
My concern is :
When 2 users visit my site and trigger a logging event (write to a logger file) simultaneously, will Elmah use something like reader/writer lock to handle race condition in the session level(other than thread level)?
The default file-based logging mechanism used by ELMAH is to create an .xml file per error - presumably to bypass locking issues writing to the same file.
If logging to the db, the default behaviour will be to lock the db table to write, but such a thing will be a split millisecond event and on the rare occasions your app triggers simultaneous errors it will be a simple queue-based scenario (not a race).
I've used ELMAH for many years, it's simple, very useful and well, just works.

Log errors in EventLog using ELMAH

I am using ELMAH to log errors in my asp.net MVC(C#) application.
I am able to log errors in xml or database. How to log errors to the eventlog using ELMAH?
ELMAH does not include an event log option.
ELMAH error log classes are not write-only; they also read the log data so that it can be displayed in the ELMAH web interface. Additionally, ELMAH logs more than just exception information. It also logs server variables, the form collection, and the information necessary to reproduce the yellow screen of death. Even if you were to log all of this information to the event log it would be difficult to read as plain text, and very difficult to read back in such a way that the ELMAH web interface could use it. If you are not going to use the ELMAH web interface then clearly that is not an issue.
If you want to log basic exception data to the event log you can create your own error log by subclassing ErrorLog. ELMAH supports multiple error logs, so you could continue to log detailed data to XML or a database (to service the ELMAH web interface) and then log a subset of that data to the event log.
Out of the box, you can't. You'd have to write a custom handler.
You can do something like this.
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("My Exception");

Resources