Serilog:无法使滚动文件正常工作

Serilog: unable to get the rollingfile working

场景:ASP.NET MVC 6 网络应用程序。

这里是project.json导入的包:

"Serilog": "2.3.0",
"Serilog.Extensions.Logging": "1.3.1",
"Serilog.Sinks.RollingFile": "3.2.0",
"Serilog.Settings.Configuration":  "2.2.0"

这里是appsettings配置

"Serilog": {
    "Using": [ "Serilog.Sinks.RollingFile" ],
    "MinimumLevel": {
        "Default": "Verbose",
        "Override": {
            "Microsoft": "Warning",
            "Microsoft.AspNetCore.Identity": "Debug",
            "System": "Warning"
        }
    },
    "WriteTo": [
        {
            "Name": "RollingFile",
            "Args": {
                "path": "logger-{Date}.log",
                "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
                "fileSizeLimitBytes": 16384
            }
        }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
        "Application": "Sample"
    }
},

这里是Startup构造函数

var builder = new ConfigurationBuilder()
                  .SetBasePath( _environment.ContentRootPath )
                  .AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true )
                  .AddJsonFile( $"appsettings.{_environment.EnvironmentName}.json", optional: true );

Configuration = builder.Build();

var logger = new LoggerConfiguration()
                 .ReadFrom.Configuration( Configuration )
                 .CreateLogger();

在配置方法中我有

loggerFactory.AddConsole();
loggerFactory.AddSerilog();

文件应该写在哪里?可能是与写入权限相关的问题?

编辑

已按照@Sanket 的建议进行操作,但仍然无效。我也切换到 kestrel 以查看它是否在控制台上工作但仍然没有运气。这是我看到的:

Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method MyCompany.MyProject.Host.Controllers.HomeController.Index MyCompany.MyProject.Host) with arguments (
(null)) - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor[1]
      Executing ViewResult, running view at path /Views/Home/Index.cshtml.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action MyCompany.MyProject.Host.Controllers.HomeController.Index (MyCompany.MyProject.Host) in 2614.9118ms

这对我来说很奇怪,因为在索引操作中的家庭控制器中我只有

public IActionResult Index() {
    _logger.LogDebug( "Index in HomeController called!" );
    return View();
}

启动时,可以这样指定滚动文件的路径-

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "Serilog-{Date}.txt"))
    .CreateLogger();

更新:

在 appsettings.json 中,将 path 参数更改为 pathFormat

"path": "logger-{Date}.log",

"pathFormat": "logger-{Date}.log",

文件将在项目位置创建。

参考这个link


更新2:

在启动构造函数中,像这样更改 serilog 记录器-

来自

var logger = new LoggerConfiguration()
                 .ReadFrom.Configuration( Configuration )
                 .CreateLogger();

Log.Logger = new LoggerConfiguration()
                 .ReadFrom.Configuration(Configuration)
                 .CreateLogger();