2 回答
TA贡献1735条经验 获得超5个赞
您可以使用 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中看到参数了:
TA贡献1784条经验 获得超9个赞
另一个可能更简单的选择是使用结构化日志记录。
例如
_logger.Info("Job {JobId} created successfully for {User} on {ReportName} with {Search}", jobId, req.UserId, req.ReportName, searchString);
这将创建事件属性 JobId、User、ReportName 和 Search。
注意:所以在这种情况下不要使用内插字符串。
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报