ASP.NET Core 1.0 logging

12.4k Views Asked by At

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

4

There are 4 best solutions below

1
On BEST ANSWER

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.

1
On

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 and stdoutLogFile.

stdoutLogEnabled If true, stdout and stderr for the process specified in processPath will be redirected to the file specified in stdoutLogFile.

And

stdoutLogFile Specifies the relative or absolute file path for which stdout and stderr from the process specified in processPath will 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.

1
On

ASP.NET Core's logging subsystem doesn't yet provide a file logger, though creating one is being discussed.

At the time of writing, using Serilog or NLog for this purpose is the way to go.

0
On

You cannot really compare the built-in logging in ASP.NET Core to ELMAH. ASP.NET Core logging is a simple logging framework, where you need to tell it what to log and when. ELMAH automatically catches all unhandled exceptions and logs a lot of contextual information about the failing request. The Seq sink actually implemented some of this behavior, which makes additional information like Protocol, RequestId and similar available through Seq, but this is not something you will get out of the box with ASP.NET logging.

ASP.NET logging is still brand new, why loggers for other frameworks and destinations will start to appear. I'm sure that file based logging will be implemented shortly and ELMAH will also be ported to ASP.NET Core for sure. For more information, check out my blog post: ASP.NET Core Logging Tutorial.