I want to implement logging in a console application. And since I'm using libraries that expect ILogger
, I need to provide one.
For reasons I don't quite fully understand, every article seems to recommend I use SeriLog rather than writing my own logger. But no one has provided a convincing reason why.
In either case, I need the following:
Can anyone tell me how I could accomplish all three, and if I really need something like SeriLog
to do so?
NOTE: I do not think it makes sense to read from the log file and use that to email the log. The log file could include logging from many sessions and I only want to email data from the most recent session.
Based on the requirements you listed, it seems Serilog would be a perfect fit for you, as you can leverage a combination of the existing sinks such as Console, File, and possibly Memory - though you don't need the last one... You can easily create a unique log file per session, read that file and send over email if that's what you want to achieve...
If you're really particular about the last part, you can write your own Serilog sink that buffers the log events the way you want and then send by email at the end.
That said, sending logs to yourself via email is not really something people do anymore these days... There are better sinks that you can use to send logs directly to a store/service that can continuously ingest logs automatically and provide great UIs and APIs to query your logs... You might want to take a look at Seq, Sentry, DataDog, and others...
Serilog is a well designed library, very widely used, and thoroughly tested, with close to 27,000 open-source projects and hundreds of contributors, as of this writing.
If you think you can innovate on how logging is done and take it to the next level, then that would be a good reason to roll your own library, otherwise seems like a waste of time reinventing the wheel.
Innovation is actually how Serilog came about... Other widely used logging libraries such as Log4Net and NLog existed before, but Serilog made semantic logging mainstream in the .NET space coupled with a well designed way of creating extensions (sinks, enrichers, destructurers, ...). The rest is history... NLog soon after followed the footsteps of Serilog, and over time Log4Net became obsolete and is legacy at this point.