记录应用程序块-记录调用者 [英] Logging Application Block - Logging the caller

查看:81
本文介绍了记录应用程序块-记录调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Log4Net进行日志记录时,很容易将称为日志的类放入日志文件。过去我发现,这很容易跟踪代码并查看类的流程。在Log4Net中,我在转换模式中使用%logger属性,如下所示:

When logging with Log4Net it's very easy to put class that called the log into the log file. I've found in the past that this makes it very easy to trace through the code and see the flow through the classes. In Log4Net I use the %logger property in the conversion pattern like so:

<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

这给了我想要的输出:

2008-09-19 15:40:26,906 [3132]错误< b> Log4NetTechDemo.Tester< / b> [(null)]-方法失败

从输出中可以看到,调用日志的类是Log4NetTechDemo.Tester,因此我可以很容易地将错误追溯到该类。

You can see from the output that the class that has called the log is Log4NetTechDemo.Tester, so I can trace the error back to that class quite easily.

在Logging Applicaton块中,我不知道如何通过简单的log调用来做到这一点。有谁知道该怎么做?如果是这样,执行该操作的示例或步骤将非常有帮助。

In the Logging Applicaton Block I cannot figure out how to do this with a simple log call. Does anyone know how it can be done? If so, an example or steps to do so would be very helpful.

推荐答案

将调用方法添加到LogEntry的ExtendedProperties词典中;当然,假设您还没有从Formatter模板中删除ExtendedProperties令牌。

Add the calling method to the LogEntry's ExtendedProperties dictionary; assuming you haven't removed the ExtendedProperties tokens from the formatter template, of course.

在日志记录包装器中放入以下内容:

Put something like this in a logging wrapper:

public void LogSomething(string msg)
{
  LogEntry le = new LogEntry { Message = msg };
  le.ExtendedProperties.Add("Called from", new StackFrame(1).GetMethod().ReflectedType);
  Logger.Write(le);
}

调用此命令将在日志末尾产生如下内容:

Calling this produces something like this at the end of the log:

Extended Properties: Called from - LAB_Demo.Tester

这篇关于记录应用程序块-记录调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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