【问题标题】:NLog with Application Insights target doesn't log custom params and exceptions带有 Application Insights 目标的 NLog 不记录自定义参数和异常
【发布时间】:2019-04-26 04:28:49
【问题描述】:

我在 WebJobs 项目中使用 Nlog 和 Application Insights 作为目标来记录遥测数据。如果我只记录如下消息,一切似乎都正常。

_logger.Log(LogLevel.Info, "Job completed");

我可以在应用程序洞察中看到跟踪信息以及消息“作业已完成”

但我想记录一些参数以及如下消息。

_logger.Info($"Job created successfully", req.UserId, req.ReportName, jobId, searchString);

或者像下面这样

catch (Exception ex)
{
    _logger.Error(ex, "Error creating the job", req.UserId, req.ReportName, searchString);
    throw;
}

我期待应用程序洞察中的跟踪包含我与消息一起传递的参数。但我只能看到消息,但看不到任何参数或异常详细信息。

我错过了什么?

编辑:

NLog nuget 版本 4.3.8 和 Microsoft.ApplicationInsights.NLogTarget nuget 版本 2.4.1

更多代码:

try
{
    var jobId = _reportingService.RequestReport(req.ReportName, searchString).Result;

    _logger.Info($"Job created successfully", req.UserId, req.ReportName, jobId, searchString);

    var output = new RetrieveReportDataRequest()
    {
        CreationRequest = new ReportCreationRequest()
        {
            ImmutableUserId = req.UserId,
           ...
        },
        SearchParameters = searchString,
        JobId = jobId,
        Created = DateTime.UtcNow,
    };

    outputQueueMessage = JsonConvert.SerializeObject(output, settings);
}
catch (Exception ex)
{
    log.WriteLine(ex.Message);
    _logger.Error(ex, "Error creating the job", req.UserId, req.ReportName, searchString);
    throw;
}

【问题讨论】:

  • 这是一个网络作业项目?你能添加更多代码吗?并且您正在使用的 nuget 包包含该版本。谢谢。
  • @IvanYang 我已经用更多细节编辑了这个问题。
  • 您好,您使用的是 webjobs nuget 包 3.0.x 还是 2.x?
  • @IvanYang Webjobs nuget 版本为 2.1.0
  • @PNDev 您可以实现自己的记录器,就像我们在工作区中所做的那样。如果您不知道如何让我知道我可以提供示例作为答案

标签: c# .net azure nlog azure-application-insights


【解决方案1】:

您可以使用LogEventInfo,然后在它的Properties中添加参数。

示例代码:

日志级别信息:

            LogEventInfo eventInfo = new LogEventInfo(LogLevel.Info, "event1", "this is a info111");
            eventInfo.Properties["myname"]= "myname is ddd";
            eventInfo.Properties["myid"] = "myid is ddd";
            eventInfo.Properties["myjobid"] = "myjobid is ddd";
            log.Log(eventInfo);

对于错误日志级别:

        #in you code, you can change the new Exception() to your own exception
        LogEventInfo eventinfo2 = new LogEventInfo(LogLevel.Error, null,null,null,null,new Exception("anexception222"));
        eventinfo2.Properties["errormessage"] = "thi si a error message";
        eventinfo2.Properties["myname"] = "myname is ddd";
        eventinfo2.Properties["myid"] = "myid is ddd";
        eventinfo2.Properties["myjobid"] = "myjobid is ddd";
        log.Log(eventinfo2);

然后就可以在azure portal中看到参数了:

【讨论】:

  • 谢谢伊万。这行得通。但是你知道为什么在这种情况下,没有 LogEventInfo 的对象数组的方法不起作用吗?
  • @PNDev,只是猜测,也许 appInsights 没有实现它。几年前,即使是 LogEventInfo.Properties 也不支持作为 appInsights 中的属性。这个user feedback 推动他们去实现它。
【解决方案2】:

另一个可能更简单的选择是使用结构化日志记录。

例如

_logger.Info("Job {JobId} created successfully for {User} on {ReportName} with {Search}", jobId, req.UserId, req.ReportName, searchString);

这将创建事件属性 JobId、User、ReportName 和 Search。

另见NLog - How to use structured logging

注意:所以在这种情况下不要使用插值字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多