简体   繁体   English

Log4Net,如何向我的日志中添加自定义字段(变量)

[英]Log4Net, how to add a custom field (variable) to my logging

I'm referring to the excellent post : 我指的是很棒的帖子:

Log4Net, how to add a custom field to my logging Log4Net,如何向我的日志中添加自定义字段

But it doesn't give me the entire solution. 但这并不能为我提供完整的解决方案。

No problem to log a string like "This is a test", but If I want to log a variable, it's responding (null). 记录“ This is a test”之类的字符串没问题,但是如果我想记录一个变量,它正在响应(空)。

Here is my snipped code not working: 这是我的摘录代码不起作用:

log4net.GlobalContext.Properties["versionid"] = Variables.IDVERSION;

Here is my working snipped code : 这是我工作的代码片段:

log4net.GlobalContext.Properties["versionid"] = " This is a test";

Although, IDVERSION is a public property systematically updated in my code c#. 虽然,IDVERSION是在我的代码c#中系统更新的公共财产。

Has anyone an idea How to solve this problem? 有谁知道如何解决这个问题? I think I'm near the solution. 我想我已经解决了。

At the moment you call this: 目前,您将其称为:

log4net.GlobalContext.Properties["versionid"] = Variables.IDVERSION;

The property will hold the value of Variables.IDVERSION at that moment . 该物业将持有的价值Variables.IDVERSION 的那一刻 It won't automagically track updates to the Variables.IDVERSION variable. 它不会自动跟踪对Variables.IDVERSION变量的更新。

So if you set Variables.IDVERSION later in code, you need to do the assignment to Properties["versionid"] again. 因此,如果稍后在代码中设置Variables.IDVERSION ,则需要再次为Properties["versionid"]进行赋值。

When log4net evaluates context properties it calls the .ToString() method of the property value. 当log4net评估上下文属性时,它将调用属性值的.ToString()方法。 So you could have dynamic property values if you have a reference as a value (I believe your idversion field is a value type eg. Int?). 因此,如果您具有引用作为值(我相信您的idversion字段是值类型,例如Int?),则可以具有动态属性值。

See https://logging.apache.org/log4net/release/manual/contexts.html for more details 有关更多详细信息,请参见https://logging.apache.org/log4net/release/manual/contexts.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM