Asp.Net 5 (core) RC1: How to log to file (rolling file logging) [DNX Core 5 compatible solution]?

How can I log to file in Asp.Net 5 RC1? I couldn’t find any solution with Microsoft.Extensions.Logging. Is there any solution which is compatible with .Net Core 5 (DNX Core 5.0)? I also was trying to use Serilog but Serilog doesn’t support core 5 yet.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To use Serilog in your ASP.NET 5 RC1 project, add the following dependencies in your project.json file:

"Serilog.Extensions.Logging": "1.0.0-rc1-final-10092",
"Serilog.Sinks.RollingFile": "2.0.0-beta-465"

Create the logger in the Startup constructor:

public Startup(IApplicationEnvironment appEnv)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.RollingFile(Path.Combine(appEnv.ApplicationBasePath, "log-{Date}.txt"))
        .CreateLogger();
}

and add Serilog in the Startup.Configure method:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog();

Method 2

Serilog.Extensions.Logging.File package is an easy way to add file logging to ASP.Net Core application (.NET Core 2.0 is supported in the latest version, which is pre-release at the moment).

  • Plugs in as ASP.NET Core logging provider
  • Provides a subset of Serilog functionality, specifically for logging to the file system.
  • Automatically pulls other Serilog packages as needed.

https://github.com/serilog/serilog-extensions-logging-file

https://www.nuget.org/packages/Serilog.Extensions.Logging.File


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x