我正在尝试在.Net Core API中实现日志记录,但是在任何地方都看不到日志文件。
我正在使用Microsoft.Extensions.Logging;和ILogger工厂方法。
在我的Program.cs中,我有...
var host = new WebHostBuilder()
.UseApplicationInsights()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
在Startup.cs文件中,我在Configure方法中拥有此文件...
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddEventSourceLogger();
在我的appsettings.json中,我有这个...
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
在我的控制器中,我注入了记录器并以这种方式调用了方法...
_logger.LogError("ERROR_MESSAGE_STRING");
正在使用错误字符串调用LogError方法,但是看不到日志。我希望可以在某处看到日志错误文件。也许在bin目录中?
答案 0 :(得分:3)
如果要登录到文件,则需要添加呼叫
loggerFactory.AddFile
如果您还没有安装记录到文件的提供程序,那么您将需要...。我没有任何具体建议(“建议我使用一个...的库”对于SO而言是不重要的),但是人们确实询问有关不使用第三方库而记录文件的信息-例如,在这里:How to log to a file without using third party logger in .Net Core?
如果您搜索“将网络核心日志记录到文件”之类的内容,那么您无疑也会带来很多成功
您已告知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
有关已配置的日志记录目标以及可在何处找到其信息的更多信息,请参见精美手册的以下页面:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#built-in-logging-providers
答案 1 :(得分:0)
只需调用LogError方法就足够了吗?
用LogError
呼叫.UseApplicationInsights()
就足够了。
我在Azure的实时指标中看到了异常,但在AI应用程序中却没有看到
对于Live Metrics
,它是实时的,您可以在发送请求后直接看到Sample Telemetry
,对于Application Insights Search
中的日志,它会有一定的依赖性,您可能考虑在大约5分钟后检查日志。