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.
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.
By default the ASP.NET Core logging if based on the standard .NET Core abstractions and the implementations for said abstractions. The link that you providing is exactly what you want to follow for consuming the logging services. These will write to the standard output (output window) for example when debugging.
The part that you're looking for specifically is the
web.config. Consider the following:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" stdoutLogFile="C:\temp\logs\log.log" /> </system.webServer> </configuration>
You are looking for the
stdoutLogEnabled If true, stdout and stderr for the process specified in
processPathwill be redirected to the file specified in
stdoutLogFile Specifies the relative or absolute file path for which stdout and stderr from the process specified in
processPathwill be logged. Relative paths are relative to the root of the site. Any path starting with â€˜.â€™ will be relative to the site root and all other paths will be treated as absolute paths.
Also see Publishing to IIS for details on the ASP.NET Core module.