【问题标题】:NLog with Application Insights - logging exceptions as an exception instead of a traceNLog 与 Application Insights - 将异常记录为异常而不是跟踪
【发布时间】:2021-01-01 17:54:29
【问题描述】:

目前我正在使用使用服务堆栈构建的 .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 Stack 提供的 Service 类之外的东西。通过如上所述定义 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 中的记录器)。

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

它所做的只是返回一个文本,上面写着“你好,名字!”

很遗憾,我无法附加调试器 - 无论出于何种原因,我都无法让它到达断点。

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

【问题讨论】:

标签: azure .net-core servicestack azure-application-insights nlog


【解决方案1】:

注意 ServiceStack 5.8 已发布,修复了该问题。

查看了 Service Stack 版本。 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。

【讨论】:

  • 感谢您打开公关!如果这样可以解决问题,我想我的选择是等待更新的包点击 NuGet,以便我可以更新我的解决方案。
  • 我会将其标记为已接受的答案,因为该更改似乎已合并回 ServiceStack 的主分支,我将等待它在 NuGet 上的发布(目前在 Myget 上可用)。跨度>
  • @JulianWenHsiLee ServiceStack 5.8 已发布,当记录为单个参数时将正确捕获异常对象。
【解决方案2】:

我可以通过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 门户 -> 应用程序洞察 -> 搜索中,此记录的消息显示在异常下。截图如下:

【讨论】:

  • 在大多数情况下,我的设置与您的设置相同,但我的结果不同。我已经用一些结果更新了问题,因为它们无法正确放入 cmets。
  • @JulianWenHsiLee,这很奇怪。你能告诉我你正在使用哪个 nuget 包和版本吗?
  • 我已将软件包添加为原始帖子的第二次编辑。
  • @JulianWenHsiLee,我用你的更新了我的 nuget,但仍然没有重现这个问题?如果可能,您介意提供更多代码吗?
  • 嗨,Ivan,我在原帖的第三次编辑中添加了更多代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2011-06-08
  • 2017-11-01
  • 1970-01-01
  • 2015-12-27
相关资源
最近更新 更多