简体   繁体   English

log4net(common.logging)错误异常

[英]log4net (common.logging) erroneous exception

I have the following log4net statement in ac# application: 我在ac#应用程序中具有以下log4net语句:

log.Info(CultureInfo.InvariantCulture, m => m(notice));

with the string contents of: 字符串内容为:

notice = "Checking: 645: Bp $B!!:{4V7r;K Bp $B$D$^$M$5$S (B <xxx@xxxxxx. Co. Jp> (B <xxxxx@xxxxxxx.Com>)"

causing this exception: 导致此异常:

[Common.Logging.Factory.AbstractLogger+FormatMessageCallbackFormattedMessage]System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. [Common.Logging.Factory.AbstractLogger + FormatMessageCallbackFormattedMessage] System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。 at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at Common.Logging.Factory.AbstractLogger.FormatMessageCallbackFormattedMessage.FormatMessage(String format, Object[] args) 在System.String.Format(IFormatProvider提供程序,String格式,Object [] args)在System.Text.StringBuilder.AppendFormat(IFormatProvider提供程序,String格式,Object [] args)在Common.Logging.Factory.AbstractLogger.FormatMessageCallbackFormattedMessage.FormatMessage (字符串格式,Object []参数)

If you notice in the string (which, in this case, is a totally piece of garbage) there is a single bracket "{". 如果您在字符串中注意到(在这种情况下,这完全是垃圾),则只有一个括号“ {”。 I'm fairly certain that this is causing the exception. 我相当确定这是导致异常的原因。 What can I do to avoid this? 我该怎么做才能避免这种情况? Escape the string somehow? 以某种方式转义字符串?

It's a fairly harmless exception, except that it shows up in the log file and is distracting. 这是一个相当无害的例外,只不过它显示在日志文件中并且分散了注意力。

It ends up that the Common.Logging log function uses the string.Format functions regardless of whether they are needed or not. 最后,Common.Logging日志函数使用string.Format函数,而不管是否需要它们。 So, as @HansKesting mentioned in comments, escaping any unintended brackets (braces) will be needed. 因此,正如@HansKesting在评论中提到的那样,需要转义所有不需要的括号(花括号)。 So, when logging data that I suspect my have this problem, I changed the code to: 因此,当记录我怀疑存在此问题的数据时,我将代码更改为:

notice = notice.Replace("{", "{{");
log.Info(CultureInfo.InvariantCulture, m => m(notice));

Hopes this helps others. 希望这对其他人有帮助。

You are using a self writen extention which accepts a Action as log argument. 您正在使用一个自写扩展名,该扩展名接受一个Action作为日志参数。 Your method m(string) causes an error. 您的方法m(string)导致错误。 You should check the source code of your method m. 您应该检查方法m的源代码。 Log4net it self will never cause any errors because it is designed to fail silent. Log4net it self永远不会导致任何错误,因为它设计为无法静音。 If the error is caused by log4net you would have find a critical bug. 如果错误是由log4net引起的,那么您将发现一个严重的错误。

The reason log4net does not accept a Action<string> as argument is that it can have side affects like this. log4net不接受Action<string>作为参数的原因是它可能会有这样的副作用。

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

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