Log4net,如何记录详细消息?
我可以毫无问题地记录信息消息,但不知道如何记录详细消息。 任何帮助都会受到欢迎。
我的问题是:
loggingEvent.Level可以在Format函数中检查。可能的值包括信息、调试、错误、详细。还有更多,但这些是我将主要使用的。
实际的日志对象只有以下方法:
Log.Info
日志.调试
日志警告
Log.Error
正如你所看到的 - 没有冗长!
那么我怎样才能记录详细消息,这与调试不同,
提前致谢
I can log info messages without a problem, but can't figure out how to log verbose messages.
Any help would be welcomed.
My problem is:
loggingEvent.Level can be checked in the Format function. The possible values are amongst others, Info, Debug, Error, Verbose. There are more, but these are the ones I'll be using mostly.
The actual log object only has the following methods:
Log.Info
Log.Debug
Log.Warn
Log.Error
As you can see - no verbose!
So how can I Log a verbose message, this is different to debug
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用扩展方法向 log4net 添加详细(或跟踪级别)。这就是我正在使用的:
使用示例:
来源:
http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx
You can add a Verbose (or Trace level) to log4net by using extension methods. This is what I'm using:
Usage example:
Source:
http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx
万一有人仍然需要答案(不使用 System.Reflection)
不需要设置 DeclaringType,只需设置 null(Lo4Net 中自动解析
)验证
代码在 log4net 中的使用
In case off someone still need the answer (without using System.Reflection)
It's not necessary to set DeclaringType, just set null (auto resolve in Lo4Net)
Tested & Validated
Code use in log4net
你无法弄清楚,因为据我所知,log4net 中没有“详细”级别。 log4j中有吗?
级别
以下是所有
调试
信息
警告
错误
致命的
OFF
信息性消息是您指定当前在应用程序中执行的操作的消息。当您说 -verbose 时,操作系统命令或工具吐出的那些消息就是此类消息。
调试消息主要供程序员使用,它们允许您编写诸如变量创建、生命周期、异常堆栈跟踪等信息。只有程序员/支持人员才会感兴趣。
[编辑]
刚刚想到这个。您可以很好地将一个名为“verbose”的开关或配置元素添加到您的应用程序中,然后在设置为 true 时输出信息消息。或者将日志记录封装在辅助方法中,该方法将登录 log4net 并将相同的消息发送到控制台。此外,您还可以使用 ConsoleAppender 将消息记录到控制台。但我从未使用过它。这是您要找的吗?
希望这有帮助。
You cannot figure out, because, AFAIK there is no "verbose" level in log4net. Is there one in log4j?
Following are the levels
ALL
DEBUG
INFO
WARN
ERROR
FATAL
OFF
Informational messages are the ones where you specify what you are doing currently in your application. Those messages spit out by OS commands or tools when you say -verbose, would be these kind of messages.
Debug messages are mostly for programmers and they allow you to write information such as variable creation, life-cycle, exception stack traces etc. Something that only the programmer/ support staff would be interested in.
[Edit]
Just thought of this. You can very well add a switch or config element to your application named "verbose" and then spit out the informational messages if set to true. Or wrap the logging in a helper method, which will log in log4net as well as send the same message to console. Also, you can use the ConsoleAppender to log messages to console. I have never used it though. Is this what you were looking for?
Hope this helps.
Apache log4net 具有以下日志级别:
对于被认为比信息性消息 (
INFO
) 更详细的消息,可以选择DEBUG
级别。编写调试消息应该像这样简单:如果您编写了非常多的调试消息和/或消息的生成成本很高(例如涉及大量字符串连接),请考虑在日志记录周围添加条件:
如果您发现自己经常这样做并且想要要干燥代码,请考虑使用 延迟消息格式化的扩展方法,这会将上面的语句变成这样:
Apache log4net has the following log levels:
For messages considered more verbose than informational messages (
INFO
), theDEBUG
level is the option to go for. Writing debug messages should be as simple as:If you write extremely many debug messages and/or the messages are costly to produce (eg involves heavy string concatenation), consider adding a conditional around the logging:
If you find yourself doing this often and want to DRY up your code, consider using extension methods for deferred message formatting, which will turn the above statement into this:
我没有尝试过,但我认为它应该非常简单:log4net 内部知道一个“详细”级别;只有 ILog 接口不公开它。因此,向此接口添加 IsVerboseEnabled 和 Verbose() 方法应该非常简单。当然,您需要愿意更改 log4net 源代码......
I did not try it, but I think it should be quite straight-forward: Internally log4net knows a level "verbose"; it is only the ILog interface that does not expose it. Therefore it should be quite simple to add a IsVerboseEnabled and Verbose() method to this interface. Of course you need to be willing to change the log4net source code...
我已经使用 BasicConfigurator 测试了 log4net,并编写了从紧急到调试的所有级别生成的日志消息输出,但对于 TRACE 或 VERBOSE 则不。
我需要执行下面的代码让他们开始记录。
I've tested log4net with BasicConfigurator, and writing log messages generated output for all levels from EMERGENCY down to DEBUG, but not for TRACE or VERBOSE.
I needed to execute the code below for them to start logging.