ASP.NET Core 1.0 logging
Asked Answered
S

4

16

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

Shelby answered 30/6, 2016 at 9:53 Comment(1)
Feel free to accept my answer if you feel it was correct, if not - let me know and I'll adjust it.Are
C
3

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.

Camarillo answered 14/2, 2017 at 0:51 Comment(1)
You can easily redirect console output to a file. There is no requirement for the API to provide explicit support for this.Lala
A
8

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.

Are answered 30/6, 2016 at 11:58 Comment(1)
Don't forget to add the folder to contain the logs in to the the solution structure and also list it in the publishOptions of project.json docs.asp.net/en/latest/hosting/directory-structure.html. Just did this and works great and names the files with a date based naming convention (eg stdout_8440_201691620225.log) . A little too verbose out of the box, but that can be tweaked.Cloddish
C
3

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.

Camarillo answered 14/2, 2017 at 0:51 Comment(1)
You can easily redirect console output to a file. There is no requirement for the API to provide explicit support for this.Lala
H
2

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.

Hypodermis answered 30/6, 2016 at 21:12 Comment(1)
And does Serilog.Sinks.MSSqlServer works with asp.net core. installing even the lastest beta version expects serilog version < 2.0.Shelby
I
2

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.

Infliction answered 7/7, 2016 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.