Log4Net 和额外字段
是否可以将额外的字段插入数据库并在 log4net 中使用它们?我想在 log-table
的额外字段中添加一个 UserId。
我已在 log4net.config
中添加了该字段:
<parameter>
<parameterName value="@userid" />
<dbType value="guid" />
<layout type="log4net.Layout.RawPropertyLayout" />
</parameter>
但是如何更新 ILog
接口以支持额外的数据库字段。所以我可以例如记录:
log4net.LogManager.GetLogger("logname").Fatal(message, exception, userid);
Is it possible to insert extra fields into the database and use them in log4net? I have a UserId I would like to have in an extra field in the log-table
.
I have added the field in the log4net.config
:
<parameter>
<parameterName value="@userid" />
<dbType value="guid" />
<layout type="log4net.Layout.RawPropertyLayout" />
</parameter>
But how do I update the ILog
interface to support the extra database field. So I could for example log:
log4net.LogManager.GetLogger("logname").Fatal(message, exception, userid);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 log4net 中的“上下文”功能。基本上,它允许您设置可以在日志附加程序中使用的属性。您可以在不同的范围(全局、线程等)设置这些属性。在您的情况下,我认为您可以(例如,在用户登录后):
在您的配置文件中,您可以使用此属性并将其添加到日志附加程序。我认为 AdoNetAppender 会是这样的。
请注意,我还没有编译上面的任何片段,因此它们可能需要一些调整,但它应该让您大致了解如何解决这个问题。
您可以在此处阅读有关此内容的更多信息。
诗。我认为第一个答案中提到的 MDC.Set 已过时。
You could use the "context" feature in log4net. Basically it allows you to set properties that you can then use in your log appender. You can set these properties at different scopes (Global, Thread etc.). In your case I think you could go (for instance, just after the user has logged in):
In your configuration file, you could then use this property and add it to the logging appender. I think it would be something like this for the AdoNetAppender
Please note that I have not compiled any of the snippets above, so they might need some tweaking, but it should give you a general idea of how this could be solved.
You can read more about this here.
Ps. I think that the MDC.Set that is referred to in the first answer is obsolete.