![](/img/trans.png)
[英]net 6.0 application: Serilog exception on startup when trying to log to Azure Application Insights
[英]Using serilog with azure application insights and .Net core
目前,我正在使用 azure application insights 直接进行日志记录,如此链接中给出的使用最新版本的 Application Insight with .net core API一切正常。
但是我现在需要借助 azure application insight 使用 serilog 进行日志记录。 甚至我也做了一些关于 serilog ( https://github.com/serilog/serilog-sinks-applicationinsights ) 的研发。 但是没有任何想法。 您能否建议我如何使用.Net core 3.0 实现这一目标
请按照以下步骤操作:
首先,安装以下软件包:
Microsoft.ApplicationInsights.AspNetCore,版本 2.14.0
Serilog.Sinks.ApplicationInsights,版本 3.1.0
Serilog.Settings.Configuration,版本 3.1.0
在 Program.cs 中:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.WriteTo.ApplicationInsights(new TelemetryConfiguration{ InstrumentationKey = "xxxxxxxxx" },TelemetryConverter.Traces)
);
}
在 controller.cs 中:
测试结果:
以下是如何为 asp.net 核心设置应用程序配置的信息,但它适用于 v6.x
https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp.net-core?tabs.netcorenew%2.netcore6
appsettings.json(示例)
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "Copy connection string from Application Insights Resource Overview"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.