gpt4 book ai didi

azure-application-insights - 使用 Serilog 和 ApplicationInsights

转载 作者:行者123 更新时间:2023-12-04 08:43:04 26 4
gpt4 key购买 nike

我正在尝试使用以下方法将 Serilog 日志记录与 Application Insights 连接起来: Serilog Sinks

我不确定为什么它似乎根本没有记录到 ApplicationInsights,而文件记录部分却按预期工作。

这是我在 Program.cs 中的代码:

public static int Main(string[] args)
{
var config = ConfigurationHelper.GetConfiguration();
var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(config);
var telemetryConfiguration = TelemetryConfiguration
.CreateDefault();
telemetryConfiguration.InstrumentationKey = config["ApplicationInsights:InstrumentationKey"];

//loggerConfiguration.WriteTo.File("log-files/libtester-web-.log",
// outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}",
// rollingInterval: RollingInterval.Day);

Log.Logger = loggerConfiguration
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Events)
.ReadFrom.Configuration(config)
.Enrich.WithMachineName()
.Enrich.WithProcessId()
.Enrich.FromLogContext()
.CreateLogger();

try
{
Log.Information("Starting web host");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog();

appsettings.json 中的相应部分:

  "Serilog": {
"Using": [
"Serilog.Sinks.ApplicationInsights"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"restrictedToMinimumLevel": "Information",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
},
{
"Name": "File",
"Args": {
"path": "log-files/libtester-web-.log",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}",
"rollingInterval": "Day",
"shared": true
}
}
],
"Enrich": [ "FromLogContext" ],
"Properties": {
"Application": "LibTester.Web"
}

它可以很好地记录到日志文件目录,但我在 AppInsights 中的资源没有获得相同的日志。

通过添加以下内容,我在输出中得到了这两条消息:'Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));'

Application Insights 遥测(未配置):{"name":"AppTraces","time":"2020-10-22T15:06:06.1140795Z","tags":{"ai.cloud.roleInstance":"MSI-970","ai.operation.id":"1b296d7eaa070941b54ef0b5019a7e18","ai.operation.parentId":"41f0aa970096b14a","ai.internal.sdkVersion":"dotnetc:2.15.0-44797"},"数据":{"baseType":"MessageData","baseData":{"ver":2,"message":"请求在 628.7215ms 200 application/json 内完成;charset=utf-8","severityLevel":"信息","properties":{"ContentType":"application/json; charset=utf-8","ProcessId":"57840","ElapsedMilliseconds":"628.7215","SpanId":"41f0aa970096b14a","Application":"LibTester.Web","RequestPath":"/weatherforecast","StatusCode":"200","ParentId":"0000000000000000","MessageTemplate":"{HostingRequestFinishedLog:l}","MachineName":"MSI -970","EventId":"{"Id":2}","TraceId":"1b296d7eaa070941b54ef0b5019a7e18","RequestId":"80000032-0003-fe00-b63f-84710c7967bb","HostingRequestFinishedLog":"请求完成628.7215 毫秒 200 应用程序/json;字符集=utf-8", “SourceContext”:“Microsoft.AspNetCore.Hosting.Diagnostics”}}}}Application Insights 遥测:{"name":"AppRequests","time":"2020-10-22T15:06:05.4273838Z","iKey":"e1281822-9ed6-4db3-8bad-0de7ce3689a7","tags": {“ai.application.ver”:“0.0.1.0”,“ai.cloud.roleInstance”:“MSI-970”,“ai.user.id”:“POM78vd9lDKUjMr10/Qhgc”,“ai.operation.id” :"1b296d7eaa070941b54ef0b5019a7e18","ai.operation.name":"GET WeatherForecast/Get","ai.location.ip":"::1","ai.internal.sdkVersion":"aspnet5c:2.15.0+2c60e729d6512c31e5791ec93c9f7726d54fe46fe ","ai.internal.nodeName":"MSI-970"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"41f0aa970096b14a","name":"GET WeatherForecast/Get","duration":"00:00:00.7411718","success":true,"responseCode":"200","url":"https://localhost:44337/weatherforecast ","properties":{"DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Requests', Ver:'1.1')","AspNetCoreEnvironment":"开发"}}}} p>

最佳答案

请尝试在 appsettings.json 中删除此部分:

  {
"Name": "ApplicationInsights",
"Args": {
"restrictedToMinimumLevel": "Information",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
},

删除后,我可以在 azure portal -> application insights 中看到日志。

这是我测试中的 appsettings.json:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"InstrumentationKey": "xxxxxxxxx"
},
"Serilog": {
"Using": [
"Serilog.Sinks.ApplicationInsights"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log-files/libtester-web-.log",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}",
"rollingInterval": "Day",
"shared": true
}
}
],
"Enrich": [ "FromLogContext" ],
"Properties": {
"Application": "LibTester.Web"
}
}
}

关于azure-application-insights - 使用 Serilog 和 ApplicationInsights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64468271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com