Log4net,如何记录详细消息?

发布于 2024-08-22 01:18:03 字数 282 浏览 7 评论 0原文

我可以毫无问题地记录信息消息,但不知道如何记录详细消息。 任何帮助都会受到欢迎。

我的问题是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏尔 2024-08-29 01:18:03

您可以使用扩展方法向 log4net 添加详细(或跟踪级别)。这就是我正在使用的:

public static class ILogExtentions
{
    public static void Trace(this ILog log, string message, Exception exception)
    {
        log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
            log4net.Core.Level.Trace, message, exception);
    }

    public static void Trace(this ILog log, string message)
    {
        log.Trace(message, null);
    }

    public static void Verbose(this ILog log, string message, Exception exception)
    {
        log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
            log4net.Core.Level.Verbose, message, exception);
    }

    public static void Verbose(this ILog log, string message)
    {
        log.Verbose(message, null);
    }

}

使用示例:

public class ClientDAO
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));

    public void GetClientByCode()
    {
        log.Trace("your verbose message here");
        //....
    }
}

来源:

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:

public static class ILogExtentions
{
    public static void Trace(this ILog log, string message, Exception exception)
    {
        log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
            log4net.Core.Level.Trace, message, exception);
    }

    public static void Trace(this ILog log, string message)
    {
        log.Trace(message, null);
    }

    public static void Verbose(this ILog log, string message, Exception exception)
    {
        log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
            log4net.Core.Level.Verbose, message, exception);
    }

    public static void Verbose(this ILog log, string message)
    {
        log.Verbose(message, null);
    }

}

Usage example:

public class ClientDAO
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));

    public void GetClientByCode()
    {
        log.Trace("your verbose message here");
        //....
    }
}

Source:

http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx

青萝楚歌 2024-08-29 01:18:03

万一有人仍然需要答案(不使用 System.Reflection)
不需要设置 DeclaringType,只需设置 null(Lo4Net 中自动解析

public bool IsVerboseEnable { get { return _log.Logger.IsEnabledFor(Level.Verbose); } }

public string Verbose(string text)
{
    _log.Logger.Log(null, Level.Verbose, text, null);
    return text;
}

)验证

代码在 log4net 中的使用

public virtual void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
    try
    {
        if (this.IsEnabledFor(level))
        {
            this.ForcedLog((callerStackBoundaryDeclaringType != null) ? callerStackBoundaryDeclaringType : Logger.declaringType, level, message, exception);
        }
    }
    catch (Exception exception2)
    {
        LogLog.Error(Logger.declaringType, "Exception while logging", exception2);
    }
}

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)

public bool IsVerboseEnable { get { return _log.Logger.IsEnabledFor(Level.Verbose); } }

public string Verbose(string text)
{
    _log.Logger.Log(null, Level.Verbose, text, null);
    return text;
}

Tested & Validated

Code use in log4net

public virtual void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
    try
    {
        if (this.IsEnabledFor(level))
        {
            this.ForcedLog((callerStackBoundaryDeclaringType != null) ? callerStackBoundaryDeclaringType : Logger.declaringType, level, message, exception);
        }
    }
    catch (Exception exception2)
    {
        LogLog.Error(Logger.declaringType, "Exception while logging", exception2);
    }
}
岁月蹉跎了容颜 2024-08-29 01:18:03

你无法弄清楚,因为据我所知,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.

一抹微笑 2024-08-29 01:18:03

Apache log4net 具有以下日志级别:

调试<信息<警告错误<致命的

对于被认为比信息性消息 (INFO) 更详细的消息,可以选择 DEBUG 级别。编写调试消息应该像这样简单:

myLog.Debug("This is a pretty verbose message");

如果您编写了非常多的调试消息和/或消息的生成成本很高(例如涉及大量字符串连接),请考虑在日志记录周围添加条件:

if (myLog.IsDebugEnabled)
{
    myLog.Debug("This is a pretty verbose message");
}

如果您发现自己经常这样做并且想要要干燥代码,请考虑使用 延迟消息格式化的扩展方法,这会将上面的语句变成这样:

Log.Debug( () => "This is a pretty verbose message" );  

Apache log4net has the following log levels:

DEBUG < INFO < WARN < ERROR < FATAL

For messages considered more verbose than informational messages (INFO), the DEBUG level is the option to go for. Writing debug messages should be as simple as:

myLog.Debug("This is a pretty verbose message");

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 (myLog.IsDebugEnabled)
{
    myLog.Debug("This is a pretty verbose message");
}

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:

Log.Debug( () => "This is a pretty verbose message" );  
小…楫夜泊 2024-08-29 01:18:03

我没有尝试过,但我认为它应该非常简单: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...

淡写薰衣草的香 2024-08-29 01:18:03

我已经使用 BasicConfigurator 测试了 log4net,并编写了从紧急到调试的所有级别生成的日志消息输出,但对于 TRACE 或 VERBOSE 则

我需要执行下面的代码让他们开始记录。

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
((Hierarchy)logRepository).Root.Level = Level.All;

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.

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
((Hierarchy)logRepository).Root.Level = Level.All;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文