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

当前,我正在使用通过Service Stack构建的.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-此内容未提及如何将类型从“跟踪”更改为“异常”

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"));

结果如下:

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

编辑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所提供的服务类。通过如上所述定义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}!" }; 
        }
    }

我们使用以下网址调用此函数:

http://localhost/hello/name

它所做的只是返回一个文本,上面写着“ Hola,name!”

不幸的是,我无法附加调试器-出于任何原因,我都无法使其达到断点。

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

penglaoda 回答:NLog与Application Insights-将异常记录为异常而不是跟踪

已经查看了服务堆栈记录器接口。这将不起作用:

// 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。

,

我可以将其记录为异常,而不是通过Log.Fatal(new NotImplementedException("test not implemented exception"));进行跟踪

我为.net核心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门户->应用程序见解->搜索中,此记录的消息显示在“异常”下。截图如下:

enter image description here

本文链接:https://www.f2er.com/3113188.html

大家都在问