如何使用树状结构进行记录 [英] How to log with a tree like structure

查看:44
本文介绍了如何使用树状结构进行记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 log4net (现在正在执行此操作)登录到文件,但是使用log4net时,只有简单的日志输出,每行都有一条消息.

I want to log with log4net (and I am doing this right now) to a file, but with log4net, there are just simple log outputs with a message each line.

现在,我想登录到相同的日志文件,但是要使用树状结构.例如,我有以下方法调用,每个方法记录其方法名称(应该有一个缩进):

Now I want to log to the same log file, but in a tree like structure. For example I've got the following method calls and each method logs its method name (there should be an indentation):

firstMethod().secondMethod().thirdMethod();

应打印此日志,例如:

2016-07-26 15:44:56,042    > firstMethod
2016-07-26 15:44:56,043         > secondMethod
2016-07-26 15:44:56,044              > thirdMethod
2016-07-26 15:44:56,045              < thirdMethod
2016-07-26 15:44:56,046         < secondMethod
2016-07-26 15:44:56,047    < firstMethod

< > 符号在方法的开头和结尾处打印在方法中.

The < and > signs are printed within the method at the start and at the end of the method.

推荐答案

您可以使用NDC实现类似的行为.但它已被弃用.现在,您可以使用上下文和上下文堆栈.查阅此文档: https://logging.apache.org/log4net/release/manual/contexts.html

You can use NDC to achieve similar behavior. But it is deprecated. Now, you can use Contexts and Contexts Stacks. Check out this documentation: https://logging.apache.org/log4net/release/manual/contexts.html

您可以定义多个堆栈(而不是缩进).一个堆栈,用于您要调试的一组行为.假设您想记录创建销售订单及其项目的日志.在此示例中,此堆栈在ThreadContext中定义.但是,您可以在GlobalContext,LogicalThreadContext或LoggingEvent中定义它.

You can define multiple stacks (instead of indentation). One stack for a set of behaviors you want to debug. Let's say you want to log creating a Sales Order and its Items. In this example, this stack is defined in ThreadContext. However you can define it in a GlobalContext , LogicalThreadContext or LoggingEvent.

using(log4net.ThreadContext.Stacks["SalesOrderLogic"].Push("SalesOrder"))
{
    log.Info("Created the sales order");

    using(log4net.ThreadContext.Stacks["SalesOrderLogic"].Push("Items"))
    {
        log.Info("Created the sales order items");
    }
}

然后应将其添加到您的配置中:

And then you should add it to your configuration:

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%logger - %property{SalesOrderStatus} - [%level]- %message%newline" />
  </layout>
</appender>

免责声明:我没有测试此代码.

DISCLAIMER: I did not test this code.

注1:旧的NDC将出现在一个线程中.因此,如果您在一个线程中推送了一条消息,则该消息将不会出现在另一个线程中.与新的上下文堆栈"方法不同,它只有一个堆栈.

Note 1: The old NDC will appear in one thread. So if you pushed a message in one thread, it won't appear in another. And it has only one stack unlike the new Context Stack method.

注2:别忘了签出上下文.这非常有用.

Note 2: Don't forget to checkout Contexts. It is very useful.

注3:如果您研究Ninject和log4net一起工作,您可能还会发现它有用

Note 3: Also you might find it useful if you research Ninject and log4net working together

这篇关于如何使用树状结构进行记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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