如果 Log4net 未正确配置,则会出现异常
按照设计,Log4net 仅在无法正确配置时才向控制台输出一条消息 (http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html#reliable)。
日志记录是我的应用程序的关键部分,因此想知道 Log4net 是否配置正确。 我无法找到任何方法从 Log4net 获取信息,所以我编写了这段代码。 但我根本不喜欢这个策略。 还有更好的建议吗?
TextWriter errorConsole = new StringWriter();
TextWriter defaultErrorConsole = Console.Error;
Console.SetError(errorConsole);
try
{
//configure Log4net
string errorMessage = errorConsole.ToString();
bool initializationError = errorMessage.IndexOf("log4net", StringComparison.OrdinalIgnoreCase) == -1;
if (initializationError)
{
throw ...
}
}
finally
{
Console.SetError(defaultErrorConsole);
}
此外,在记录消息时,例如调用 ILog.Error。 有没有办法查看是否已记录?
By design Log4net only outputs a message to the console if it can't be configured correctly (http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html#reliable).
Logging is a critical part of my application, so want to know if Log4net was configured correctly. I've not been able to find any way to get the info from Log4net, so I wrote this code. But I don't like the strategy at all.
Any better suggestions?
TextWriter errorConsole = new StringWriter();
TextWriter defaultErrorConsole = Console.Error;
Console.SetError(errorConsole);
try
{
//configure Log4net
string errorMessage = errorConsole.ToString();
bool initializationError = errorMessage.IndexOf("log4net", StringComparison.OrdinalIgnoreCase) == -1;
if (initializationError)
{
throw ...
}
}
finally
{
Console.SetError(defaultErrorConsole);
}
Also when logging a message, such as calling ILog.Error. Is there a way to see if it was logged?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
配置期间发生的任何错误都会使用 log4net.Util 在 log4net 内部记录.LogLog类。 不幸的是,在当前版本中,此类并未公开太多可以帮助您的内容。 在当前源另一方面,您现在可以订阅 LogReceived 事件,每当收到内部日志消息时就会触发该事件。
记录器存储库还通过 ILoggerRepository.ConfigurationMessages() 方法进行了扩展,该方法公开了最新的内部消息列表。
关于监视消息是否已记录:附加程序(至少是在 AppenderSkeleton 类上构建的附加程序)使用与配置期间使用的相同的内部错误消息传递。 因此,使用最新的源,您将通过 LogReceived 事件收到任何此类通知。
Any errors that occurs during configuration is logged internally in log4net using the log4net.Util.LogLog class. Unfortunately, in the current release this class does not expose very much that can help you. In the current source on the other hand, you can now subscribe to a LogReceived event which fires whenever an internal log message is received.
The logger repository have also been extended with the ILoggerRepository.ConfigurationMessages() method which exposes the latest list of internal messages.
Regarding monitoring whether a message was logged or not: appenders, at least those built on the AppenderSkeleton class, uses the same internal error messaging as used during configuration. So with the latest source you will receive any such notifications via the LogReceived event.