1

我当前的任务:我需要从 OutputTemplate 中的 {Exception} 对象中删除 StackTrace

"WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:yyyy:MM:dd hh:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}"
        }
      }]

异常对象包含 Message AND St​​ackTrace。我需要删除 StackTrace。可能吗?

我在自定义 Enricher 中尝试过自定义属性:

public class ExceptionEnricherTest : ILogEventEnricher
     {
          public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
          {
              if (logEvent.Exception == null)
              {
                  return;
              }
    
         var logEventProperty = propertyFactory.CreateProperty("ExceptionWithoutStackTrace", logEvent.Exception.Message);
    
            logEvent.AddPropertyIfAbsent(logEventProperty);
          }
     }
public static class LoggingExtensions
    {
        public static LoggerConfiguration WithExceptionEnricher(
            this LoggerEnrichmentConfiguration enrich)
        {
            if (enrich == null)
                throw new ArgumentNullException(nameof(enrich));

            return enrich.With<ExceptionEnricherTest>();
        }
    }

在这种情况下的应用设置:

"Enrich": [ "FromLogContext", "WithCorrelationId", "WithExceptionEnricher" ]

"WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:yyyy:MM:dd hh:mm:ss} {CorrelationId} {ClientIp} {Level:u3}] {Message:lj}{NewLine}{ExceptionWithoutStackTrace}"
        }
      }]

但我没有看到我的自定义属性。

4

0 回答 0