【问题标题】:How can I configure ILogger to output the logs to a specific file?如何配置 ILogger 以将日志输出到特定文件?
【发布时间】:2018-08-16 22:52:31
【问题描述】:

我有一个要部署到生产环境的 ASP.NET Core 2 Web 应用程序,并且需要启用日志来解决错误。我在文档中找不到关于 appsettings.json 记录器部分中的架构的任何地方。这就是我所拥有的:

"Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },"Console": {
      "IncludeScopes": "true"
    }
  }

当我从控制台运行 exe 时,日志信息显示在控制台中,但实体框架也在记录,所以我看不到我自己的消息,因为显示的输出量是有限的。因此我需要将日志输出到一个文件中,比如 C:\temp\my.log。如何配置 ASP.NET Core 2 来执行此操作?

提前致谢。

【问题讨论】:

  • 我认为您必须将 stdoutLogEnabled 设置为 truestdoutLogFile 到已部署应用程序的 web.config 中的某个有效文件夹路径
  • 我相信您需要添加第三方记录器才能写入文件。最容易添加的一个是Serilog。它有一个接收器到write to a file

标签: logging asp.net-core asp.net-core-2.0


【解决方案1】:

因此我需要将日志输出到一个文件,比如 C:\temp\my.log。如何配置 ASP.NET Core 2 来执行此操作?

Serilog 有a package for that

dotnet add package Serilog.Extensions.Logging.File

我们是这样配置的。

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging();
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 
{
    loggerFactory.AddFile("C:\\temp\\my.log");
}

另见:https://nblumhardt.com/2016/10/aspnet-core-file-logger/

【讨论】:

    【解决方案2】:

    This issue 似乎和你的要求一样。

    您也可以尝试使用 NLog 将日志输出到文件。它还可以确定其配置文件中的日志布局。

    installing之后,在startup.cs中设置logs文件夹:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            GlobalDiagnosticsContext.Set("configDir", "C:\\git\\damienbod\\AspNetCoreNlog\\Logs");
            GlobalDiagnosticsContext.Set("connectionString", Configuration.GetConnectionString("NLogDb"));
    
            loggerFactory.AddNLog();
    
            app.UseMvc();
        }
    

    然后在 nlog.config 中,设置文件名:

     <target xsi:type="File" name="allfile" fileName="${gdc:item=configDir}\nlog-all.log"/>
    

    以下是您可以参考的示例: https://github.com/damienbod/AspNetCoreNlog

    【讨论】:

    • 谢谢!我最终按照您的建议使用了 NLog。不敢相信需要第三方工具来解决这个问题,我原以为这是一种非常常见的情况。
    【解决方案3】:

    它使用中间件的另一种方式是如何

    https://gist.github.com/elanderson/c50b2107de8ee2ed856353dfed9168a2

    (请阅读cmets,因为代码中存在一些问题,cmets关于body seek position)

    并设置 web.config 参数标准输出,如 stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
          </handlers>
          <aspNetCore processPath="dotnet" arguments=".\ReportViewer.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
        </system.webServer>
      </location>
    </configuration>
    

    您应该在项目目录中创建两个文件夹logslogs\stdout

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2012-12-21
      • 2011-09-18
      相关资源
      最近更新 更多