azure - 将 Serilog 中的 outputTemplate 参数与 Azure Application Insights 结合使用

标签 azure asp.net-core logging azure-application-insights serilog

我目前正在使用 Serilog 实现 Azure Application Insights 日志记录,除非我在 Serilog 配置中使用输出模板,否则该日志记录工作正常。将 Serilog 数据传递给应用程序洞察时,模板似乎被忽略了。

appsetting.json 中的我的 serilog 配置:

"Serilog": {
    "Using": [ "Serilog.Sinks.ApplicationInsights" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "logs\\log-{Date}.txt",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
        }
      },
      {
        "Name": "ApplicationInsights",
        "Args": {
          "restrictedToMinimumLevel": "Information",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
          "outputTemplate": "Test Template - {Message}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
      "Application": "app"
    }
  },

日志记录语句:

logger.Error("测试 AI 的 Serilog 错误 - "+ DateTime.Now);

应用程序洞察中的输出:

enter image description here

这是为应用程序见解自定义错误消息的正确方法吗?

最佳答案

查看源码后serilog-sinks-applicationinsights ,你会发现它没有从appsetting.json读取outputTemplate

要解决此问题,您可以实现自定义 TemplateTraceTelemetryConverter

  1. TemplateTraceTelemetryConverter

    public class TemplateTraceTelemetryConverter : TraceTelemetryConverter
    {
        public override IEnumerable<ITelemetry> Convert(LogEvent logEvent, IFormatProvider formatProvider)
        {
            var templateParser = new MessageTemplateParser();
            var template = templateParser.Parse($"Test Template - {logEvent.MessageTemplate.Text}");
            LogEvent newLogEvent = new LogEvent(logEvent.Timestamp
                , logEvent.Level
                , logEvent.Exception
                , template
                , logEvent.Properties.Select(p => new LogEventProperty(p.Key, p.Value)));
            return base.Convert(newLogEvent, formatProvider);
        }
    }
    
  2. 使用TemplateTraceTelemetryConverter

    "Serilog": {
        "Using": [
        "Serilog.Sinks.ApplicationInsights",
        ],
        "MinimumLevel": "Debug",
        "WriteTo": [
        {
            "Name": "Console",
            "Args": {
            "outputTemplate": "Test Template - {Message}"
            }
        },
        {
            "Name": "RollingFile",
            "Args": {
            "pathFormat": "logs\\log-{Date}.txt",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
            }
        },
        {
            "Name": "ApplicationInsights",
            "Args": {
            "restrictedToMinimumLevel": "Error",          
            "telemetryConverter": "YourProjectNamespace.TemplateTraceTelemetryConverter, YourProjectNamespace"
            //"outputTemplate": "Test Template - {Message}"
            }
        }
        ],
        "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
        "Properties": {
        "Application": "app"
        }
    }
    

关于azure - 将 Serilog 中的 outputTemplate 参数与 Azure Application Insights 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57183780/

相关文章:

node.js - 如何在 Node.js 中使用 log.io 日志模块

java - 如何从单个 Docker 容器中捕获多个日志流?

oracle - 以 Oracle 作为源在 Synapse 专用池中加载 6 亿多条记录

powershell - 如何通过 powershell 创建 Windows 服务 ACS?服务器无法验证请求

Azure Cosmos DB 要求提供存储过程的分区键

c# - ASP.NET Core Identity - 使 token (电子邮件确认、密码重置等)在不同服务器上有效?

c# - 在 ASP.NET Core MVC 中同时验证两个属性

azure - 网站与新 Azure 门户中的 webrole 应用程序有何不同

asp.net-core - 访问被拒绝的查询字符串太长

java - log4j2 健康信息和错误处理