loggingasp.net-coreentity-framework-coreserilogasp.net-core-logging

ASP.NET Core 1.0 logging


I have ASP.NET Core web api project. Now I want to log the errors and events. I previously used ELMAH in my project but it seems that elmah is not available for ASP.NET Core. I referred to this Official link for configuring the default logging service provided by Microsoft. I see nowhere how could I save these logs in the text file or in database.

If ASP.NET Core already has default logging functionality, I am wondering why should I use other tools like elmah, log4net. So again when I searched for article that implements default logging to save the logs in db or in text file, I couldn't find any. Is there any way how we can save the logs in file using ASP.NET core's built in support for logging?

I am currently using Serilog which works perfectly and also downloaded seq for displaying the logs gracefully in browser. However, I am still wondering how could I achieve the same using built in asp.net core logging functionality.

Log File using Serilog:

Logs displayed using Seq: enter image description here


Solution

  • I just did a bunch of research on this topic for a blog post on ASP.NET Core logging. I looked at the built in logging as well as NLog, SeriLog, and log4net.

    Basically what I found is the the built in ILoggerFactory works fine but does has one glaring problem: it won't write to file. It supports Console, Debug, ETW, and some other providers, but not files. I assume because files are quite complicated when you have to start worrying about max file sizes, rotations, and all the other stuff that goes with it.

    The built in logging works great for .NET internals and since they don't need to write to files, it really isn't a limitation for the .NET team. Serilog and NLog both provide little extensions to enable the file writing. Of course those libraries also provide a lot more functionality across the board.

    Serilog's extension requires one line of code and it adds file logging. You can read about it here: https://github.com/serilog/serilog-extensions-logging-file

    Here is my blog post about ASP.NET Core logging if it helps: https://stackify.com/asp-net-core-logging-what-changed/

    I'd say if you have really simple logging needs you can make the built-in logging work, with the File extension. As soon as you want to get in to any advanced functions like controlling the output format, logging to external services, file rotation, max file size, etc, you will want to use NLog or Serilog.