带有 AppInsights 的 SeriLog 不写入 AppInsights [英] SeriLog with AppInsights not writing to AppInsights

查看:21
本文介绍了带有 AppInsights 的 SeriLog 不写入 AppInsights的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个 .NET CORE 2.0 项目上使用 Serilog 和 appinsights 编写日志.

I am writing logs using Serilog with appinsights on an .NET CORE 2.0 project.

我已经按如下方式配置了 SeriLog,

i have configured SeriLog as follows,

var loggerConfiguration = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .Enrich.WithDemystifiedStackTraces();

并按如下方式写入 appInsights,

and writing to appInsights as follows,

 loggerConfiguration = loggerConfiguration.WriteTo.ApplicationInsightsTraces(appInsightsIntrumentationKey, serilogLevel)
.WriteTo.RollingFile(Path.Combine(contentRoot, "Logs/log-{Date}.log"), retainedFileCountLimit: 14);

我在日志文件夹中看到生成的日志,但在 appInsights 中没有看到任何内容.

i see the log generated inside the logs folder, but i dont see anything in appInsights.

我做错了什么?

推荐答案

我已经尝试过 如文档所述

    "ApplicationInsights": {
        "InstrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
        "EnableAdaptiveSampling": false,
        "EnablePerformanceCounterCollectionModule": false
    },
    "Serilog": {
        "WriteTo": [
            { "Name": "Console" },
            {
                "Name": "ApplicationInsights",
                "Args": {
                    "instrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
                    "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
                }
            }
        ]
    }

它适用于 Azure 应用服务和开发计算机

it works from both Azure App Service and dev computer

Program.cs:

Program.cs:

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

Startup.cs

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
}

示例控制器:

    [Route("[controller]")]
    [ApiController]
    public class BadValuesController : ControllerBase
    {
        private readonly ILogger<BadValuesController> _logger;
        public BadValuesController(ILogger<BadValuesController> logger)
        {
            _logger = logger;
            _logger.LogDebug("ctor");
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> GetData()
        {
            _logger.LogWarning("going to raise 6 from local app");
            throw new NotImplementedException();
        }
    }

不需要重新构建应用程序,因为没有执行 services.AddApplicationInsightsTelemetry();.您可以将 dll 文件复制到您的应用程序并调整配置以打开 AppInsights 登录.

There is no need to rebuild application since no services.AddApplicationInsightsTelemetry(); is executed. You may just copy dll files to your application and adjust config to turn the AppInsights logging on.

但是你没有 如果没有修改源代码,则拥有 TelemetryClient 实例.所以你必须等待一段时间才能选择应用洞察日志.

However you do not have TelemetryClient instance if there is no source code modifications. So you have to wait a while before can select app insights log.

我已经安装了 NuGet 包

I've installed NuGet packages

  • Microsoft.ApplicationInsights.AspNetCore
  • Serilog.AspNetCore
  • Serilog.Enrichers.Environment
  • Serilog.Enrichers.Process
  • Serilog.Sinks.ApplicationInsight

这篇关于带有 AppInsights 的 SeriLog 不写入 AppInsights的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆