具有 Application Insights 目标的 NLog 不记录自定义参数和异常
NLog with Application Insights target doesn't log custom params and exceptions
我在 WebJobs 项目中使用带有 Application Insights 的 Nlog 作为目标来记录遥测。如果我只记录如下消息,一切似乎都正常。
_logger.Log(LogLevel.Info, "Job completed");
我可以在 application insights 中看到 Trace 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;
}
您可以使用 LogEventInfo,然后在其属性中添加参数。
示例代码:
对于日志级别信息:
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里面看到参数了:
另一个可能更简单的选项是使用结构化日志记录。
例如
_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
注意:所以在这种情况下不要使用内插字符串。
我在 WebJobs 项目中使用带有 Application Insights 的 Nlog 作为目标来记录遥测。如果我只记录如下消息,一切似乎都正常。
_logger.Log(LogLevel.Info, "Job completed");
我可以在 application insights 中看到 Trace 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;
}
您可以使用 LogEventInfo,然后在其属性中添加参数。
示例代码:
对于日志级别信息:
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里面看到参数了:
另一个可能更简单的选项是使用结构化日志记录。
例如
_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
注意:所以在这种情况下不要使用内插字符串。