.Net Core Logging 中的日志文件写入何处

Joh*_*ohn 5 c# asp.net-core

我正在尝试在 .Net Core API 中实现日志记录,但在任何地方都看不到日志文件。

我正在使用 Microsoft.Extensions.Logging;和 ILogger 工厂方法。

在我的 Program.cs 中我有...

   var host = new WebHostBuilder()
           .UseApplicationInsights()
           .UseKestrel()
           .UseContentRoot(Directory.GetCurrentDirectory())
           .UseIISIntegration()
           .UseStartup<Startup>()
           .Build();

        host.Run();
Run Code Online (Sandbox Code Playgroud)

在我的 Startup.cs 中,我在配置方法中有这个...

        loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddEventSourceLogger();
Run Code Online (Sandbox Code Playgroud)

在我的 appsettings.json 中我有这个...

  "Logging": {
   "IncludeScopes": false,
  "LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
   }
  }
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我注入记录器并以这种方式调用该方法......

   _logger.LogError("ERROR_MESSAGE_STRING");
Run Code Online (Sandbox Code Playgroud)

正在使用错误字符串调用 LogError 方法,但我没有看到日志。我希望在某个地方看到日志错误文件。也许在 bin 目录中?

Cai*_*ard 5

如果你想记录到一个文件,那么你需要添加一个调用

loggerFactory.AddFile
Run Code Online (Sandbox Code Playgroud)

如果你还没有安装一个记录到文件的提供程序,那么你需要......我没有任何具体的建议(“推荐我一个库......”对于SO来说不是主题),但人们确实会问在不使用第三方库的情况下记录到文件 - 例如:How to log to a file without usingthird party logger in .Net Core?

如果您搜索“net core log to file”之类的内容,毫无疑问您也会找到大量的搜索结果

您告诉 loggerFactory 包含在制造的记录器中的位置目前没有基于文件的接收器。每次您的工厂制造记录仪时,都会生产出针对以下目标的记录仪:

loggerFactory.AddApplicationInsights //Microsoft azure cloud application insights service            
loggerFactory.AddConsole //the standard output of the command line
loggerFactory.AddDebug //for example, visual studio debug output window (i think)
loggerFactory.AddEventSourceLogger //for example, the windows performance monitoring system
Run Code Online (Sandbox Code Playgroud)

有关您已配置的日志记录目标以及在哪里可以找到其信息的更多信息,请参阅精美手册的此页面:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/ ?view= aspnetcore-2.2#built-in-logging-providers