[英]Using serilog with azure application insights and .Net core
Currently, I am using azure application insights directly for logging as given in this link Use latest version of Application Insight with .net core API and everything is working fine.目前,我正在使用 azure application insights 直接进行日志记录,如此链接中给出的使用最新版本的 Application Insight with .net core API一切正常。
But I need to use the serilog for logging now with the help of azure application insight.但是我现在需要借助 azure application insight 使用 serilog 进行日志记录。 Even I do some R&D about serilog ( https://github.com/serilog/serilog-sinks-applicationinsights ).
甚至我也做了一些关于 serilog ( https://github.com/serilog/serilog-sinks-applicationinsights ) 的研发。 But didn't get any idea.
但是没有任何想法。 Could you please suggest me that how can we achieve that with the.Net core 3.0
您能否建议我如何使用.Net core 3.0 实现这一目标
Please follow the steps below:请按照以下步骤操作:
First, in stall the following packages:首先,安装以下软件包:
Microsoft.ApplicationInsights.AspNetCore, version 2.14.0 Microsoft.ApplicationInsights.AspNetCore,版本 2.14.0
Serilog.AspNetCore, version 3.2.0 Serilog.AspNetCore,版本 3.2.0
Serilog.Sinks.ApplicationInsights, version 3.1.0 Serilog.Sinks.ApplicationInsights,版本 3.1.0
Serilog.Settings.Configuration, version 3.1.0 Serilog.Settings.Configuration,版本 3.1.0
In Program.cs:在 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)
);
}
In controller.cs :在 controller.cs 中:
The test result :测试结果:
Here is information how to setup app configuration for asp.net core, but it for v6.x以下是如何为 asp.net 核心设置应用程序配置的信息,但它适用于 v6.x
https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp.net-core?tabs.netcorenew%2.netcore6 https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp.net-core?tabs.netcorenew%2.netcore6
appsettings.json (example) 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.