登录与log4net的与日志级别的参数 [英] Log with log4net with the loglevel as parameter

查看:309
本文介绍了登录与log4net的与日志级别的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有登录与log4net的和使用中的LogLevel为参数的方法吗?



这是不是写


$ B $, b

  Log.Debug(出事了); 



我想这样写:

 日志(出事了,LogLevel.Debug); 


解决方案

据log4net的文档的这里(下看看 log4net.Core.ILogger ),您可以使用ILogger接口上的登录方法。

 私有静态的ILog记录= 
LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod()DeclaringType)。

logger.Logger.Log(logger.GetType(),LogLevel.Debug,出事了,前);

的类型参数用于由log4net的确定日志记录代码和应用程​​序之间的调用堆栈的边界码。如果启用了方法名日志,log4net的导航调用堆栈,直到一个MethodInfo的堆栈上的DeclaringType等于型(在上面的日志呼叫类型)通过。当它发现DeclaringType,在调用堆栈中的下一个方法是实际调用方法(应用程序代码)。



您还可以使用,需要一个<$ C超载$ C> LoggingEvent所结构。



该文档还指出,Log方法的目的是通过包装使用。我不知道这是否应该被视为一个信息的消息或强建议不要直接使用它。



如果你想使你的所有日志记录通过日志的方法调用,你可以改变你在哪里得到的记录是这样的代码(这样可以消除使用记录器属性每个日志调用):

 私有静态ILogger记录= 
LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod()DeclaringType).Logger;

logger.Log(logger.GetType(),LogLevel.Debug,出事了,前);


Is there a way to log with log4net and use the LogLevel as parameter?

That is, instead of writing

Log.Debug("Something went wrong");

I would like to write something like this:

Log("Something went wrong", LogLevel.Debug);

解决方案

According to the log4net documentation here (look under log4net.Core.ILogger), you can use the Log method on the ILogger interface.

private static ILog logger = 
    LogManager.GetLogger(
    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

logger.Logger.Log(logger.GetType(),LogLevel.Debug,"Something went wrong", ex);

The Type parameter is used by for log4net to determine the boundary in the call stack between logging code and application code. If you have method name logging enabled, log4net navigates up the call stack until the DeclaringType of the MethodInfo on the stack is equal to the passed in Type (the Type in the Log call above). When it finds that DeclaringType, the next method in the call stack is the actual calling method (application code).

You can also use the overload that takes a LoggingEvent structure.

The documentation also says that the Log method is intended to be used by wrappers. I don't know if that should be regarded as an "informational" message or a strong suggestion to not use it directly.

If you want to make all of your logging calls via the Log method, you can change the code where you get the logger to be like this (so you can eliminate using the Logger property for every Log call):

private static ILogger logger = 
    LogManager.GetLogger(
    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Logger;

logger.Log(logger.GetType(),LogLevel.Debug,"Something went wrong", ex);

这篇关于登录与log4net的与日志级别的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆