NLog with Application Insights - 将异常记录为异常而不是跟踪

NLog with Application Insights - logging exceptions as an exception instead of a trace

目前我正在使用使用服务堆栈构建的 .NET Core Web 应用程序。当前使用 NLog 提供日志记录,以 Azure Application Insights 作为目标。

目前,当我记录消息和异常时,我遇到了一些奇怪的行为:

Log.Fatal("test not implemented exception", new NotImplementedException()); //this logs under Exceptions in Application Insights
Log.Fatal(new NotImplementedException("test not implemented exception")); //this logs under Trace

我想要的是能够将第二行记录为异常而不是跟踪。有什么办法可以实现吗?

我找不到这个问题的答案,所以我发帖作为问题。

NLog exceptions as Trace in Application Insights - 这个没有提到如何将类型从 Trace 更改为 Exception

Application Insights - Logging exceptions - 这个没有提到NLog

https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/102 - 这是最接近我需要的,但是没有更新

编辑:因为评论标记无法正确显示图像和格式,我一直在使用以下代码行进行测试:

Log.Fatal("test no message - fatal", new NotImplementedException());
Log.Error(new NotImplementedException("test no message - error"));

结果如下:

编辑 2:包版本如下:

NLog (4.6.8)
NLog.Web.AspNetCore (4.9.0)
Microsoft.ApplicationInsights.AspNetCore (2.8.2)
Microsoft.ApplicationInsights.NLogTarget (2.11.0)

编辑 3:更多代码:

我们的服务接口层使用了这段代码。它实现了 Service class 之外的东西,它由 Service Stack 提供。通过如上定义 LogFactory,我们可以将其传递到我们的服务层。

此服务接口托管在位于同一解决方案下的单独项目中(我们称之为 Api.ServiceInterface)。

AppServiceBase.cs

public abstract class AppServiceBase : Service
    {
        protected ILog Log { get; set; }
        protected ICacheClient CacheClient { get; set; }

        protected AppServiceBase()
        {
            Log = LogManager.GetLogger(GetType());
        }

        public AppServiceBase(ICacheClient cacheClient) : this()
        {
            CacheClient = cacheClient;
        }

        public UserSession UserSession => this.GetSession() as UserSession;
    }

HelloService.cs(从AppServiceBase扩展出来的服务,可以直接调用上面AppServiceBase中的logger)

public class HelloService : AppServiceBase
    {
        public object Any(Hello request)
        {
            Log.Fatal("test no message - fatal 1", new NotImplementedException());
            Log.Error(new NotImplementedException("test no message - error 1"));
            return new HelloResponse { Result = $"Hola, {request.Name}!" }; 
        }
    }

我们使用以下方法调用此函数 URL:

http://localhost/hello/name

它所做的只是 return 一段文字 "Hola, name!"

不幸的是,我无法附加调试器 - 无论出于何种原因,我都无法让它命中断点。

我不知道我是否也需要在 Api.ServiceInterface 项目中使用相同的库。请注意,Api.ServiceInterface 是一个 Class 库类型的项目,在 .NET Standard 2.0 上运行。

我可以通过 Log.Fatal(new NotImplementedException("test not implemented exception"));

将其记录为异常而不是跟踪

我为.net core web项目安装的nuget包:

Microsoft.ApplicationInsights.NLogTarget, version 2.11.0

Microsoft.ApplicationInsights.AspNetCore, version 2.8.2

这是我项目中的NLog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
  </extensions>
  <targets>
    <target xsi:type="ApplicationInsightsTarget" name="aiTarget">
      <!--<instrumentationKey>Your_Resource_Key</instrumentationKey>-->
      <!-- Only required if not using ApplicationInsights.config -->
      <contextproperty name="threadid" layout="${threadid}" />
      <!-- Can be repeated with more context -->
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="aiTarget" />
  </rules>
</nlog>

并且在 HomeController.cs -> Index() 方法中:

        public IActionResult Index()
        {
            Logger logger = LogManager.GetLogger("HomeController");
            logger.Fatal(new NotImplementedException("test not implemented exception, only one new exception() 222"));
            return View();
        }

执行代码后,在 azure portal -> application insights -> Search 中,此记录的消息显示在 Exception 下。截图如下:

注意 ServiceStack 5.8 已经发布,解决了这个问题。

看过服务堆栈版本。 5.7 记录器接口。这行不通:

// Will pass exception a string.Format parameter
Log.Fatal("test no message - fatal", new NotImplementedException());
// Will make Service Stack call Exception.ToString
Log.Error(new NotImplementedException("test no message - error"));

并且您需要解决此问题才能正确访问 NLog:

Log.Fatal(new NotImplementedException(), "test no message - fatal");
Log.Error(new NotImplementedException("test no message - error"), ex.Message);

我创建了以下 PR https://github.com/ServiceStack/ServiceStack/pull/1210,因此当单独调用异常对象时,服务堆栈将正确地将异常对象传递给 NLog。