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

雨燕双飞 提交于 2019-12-01 17:04:16

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();

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!