使用 log4net 时可以控制其他参数吗?
在我的代码中,我使用 log4net 进行数据库日志记录。 我注意到执行日志记录的类有这段代码。
public class Logger
{
readonly log4net.ILog _log;
public Logger()
{
XmlConfigurator.Configure();
_log = log4net.LogManager.GetLogger("Oracle_Log_Appender");
}
public void Log(string logString)
{
_log.Debug(logString);
}
}
我传递给该机制的字符串被插入到消息列中的日志表中。我想要多一列,其中包含 ID 值,使我能够在表中搜索正确的消息。我能想到的例子是: 我正在研究一系列客户对象。每个对象都有一个带有 ID 的主键。 如果其中任何一个失败,我想以以下方式记录它。
OBJECT_ID MESSAGE OBJECT_TYPE
1001 Failed to fetch credit report Customer
我可以使用 log4net 来做到这一点吗?
In my code I have Db logging using log4net.
I noticed that the class that does the logging has this code.
public class Logger
{
readonly log4net.ILog _log;
public Logger()
{
XmlConfigurator.Configure();
_log = log4net.LogManager.GetLogger("Oracle_Log_Appender");
}
public void Log(string logString)
{
_log.Debug(logString);
}
}
The string that I pass to this mechanism gets inserted to a log table in the message column. I would like to have one more column which has ID values that will enable me to search the table for the correct message. The example I can think of is:
I am working on a series of customer objects. Each object has a primary key with an ID.
If any of them fail, I want to log it in the following fashion.
OBJECT_ID MESSAGE OBJECT_TYPE
1001 Failed to fetch credit report Customer
Can I do this using log4net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您浏览同一主题的旧问题(带有答案!)时...此代码示例不是惯用 log4net。
ILog
实例应该是静态只读的,并由它们所在的类命名。通过执行日志记录的类的命名空间来管理您的附加程序、过滤器和级别。不是记录器中的单个名称。因此,请确保您的数据库访问全部位于同一名称空间(或名称空间根)中。例如
也没有理由隐藏这个愚蠢的包装类后面丰富的
ILog
接口。While you browse an older question (with answers!) on the same topic... this code sample is not idiomatic log4net.
ILog
instances should be static readonly and named by the class they are in.Manage your appenders, filters, and levels by the namespace of the classes doing the logging. Not by a single name in the logger. So, make sure your db access is all in the same namespace (or namespace root). For example
There's also no reason to hide the rich
ILog
interface behind this goofy wrapper class.