NLog可以通过c#扩展方法保留呼叫站点信息吗? [英] Can NLog preserve callsite information through c# extension methods?

查看:181
本文介绍了NLog可以通过c#扩展方法保留呼叫站点信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管相似,但这与有关使用NLog包装器的问题不同.扩展方法增加了另一种间接访问级别,这甚至使正确的包装程序也可以报告错误的呼叫站点

我目前在NLog周围使用日志记录包装程序,并且使用它们在示例中显示的技巧来获取准确的呼叫站点信息.对于我开始的一个新项目,我想创建一个更简单的类,因此我只实现了以下接口:

I currently use a logging wrapper around NLog, and I use the trick they show in the example in their source for getting accurate callsite info. For a new project I started, I wanted to create a simpler class, so I just implemented something like the following interface:

public interface ILogger
{
    void Log( LogEntry entry );
}

然后我创建了一个扩展方法类,如:

And then I created an extension method class like:

public static class LoggerExtensions
{
    public static void Debug( this ILogger logger, string format, params object[] args)
    {
        logger.Log( new LogEntry( LogLevel.Debug, format, args ) );
    }

    ...
}

问题在于NLog然后将呼叫站点显示为扩展方法,而不是扩展方法的调用方.我做了一些搜索,但找不到有关NLog和扩展方法的任何信息.

The problem is that NLog then shows the callsite as the extension method instead of the caller of the extension method. I did a little searching around but couldn't find anything about NLog and extension methods.

在这种情况下是否可以修复呼叫站点信息?还是在界面本身中包含Debug,Info等功能的唯一方法?

Is it possible to fix the callsite information in this case? Or is the only way to include the Debug,Info, etc functions in the interface itself?

推荐答案

晚些时候参加聚会,但是由于我对包装程序使用了扩展方法,因此我对此解决方案有疑问.通过将程序集传递给LogManager,我可以使NLog忽略我的扩展类.

Late to the party but I had issue with this solution given that I use extension methods for my wrapper. By passing the assembly to LogManager I was able to get NLog to ignore my extension class.

    /// <summary>
    /// Bootstrap the wrapper class
    /// </summary>
    static Logger()
    {
        LogManager.AddHiddenAssembly(typeof(LoggingExtensions).Assembly);
    }

除了

添加给定的程序集,当NLog尝试在堆栈跟踪中查找调用方法时,将跳过该程序集.

Adds the given assembly which will be skipped when NLog is trying to find the calling method on stack trace.

来自 NLog文档

通过此设置,我什至设法获得了扩展方法+与SimpleInjector一起使用的DI.

With this setup, I've even managed to get extension methods + DI working with SimpleInjector.

要显示您仍然可以使用此方法在同一程序集中拥有一个呼叫站点

我的Logger()和SettingsHelper()一起存在于实用程序项目中 我使用SettingsHelper的输出设置了测试:

My Logger() lives in a Utilities project along with a SettingsHelper() I setup a test with output from SettingsHelper:

2015-08-18 20:44:07.5352 | vstest.executionengine.x86 |调试| Utilities.Settings.SettingsHelper |与Logger()来自同一程序集的测试| ActionTests.LoggingTest + LogTest.RunLogTest

黑体字是$ {callsite}

The bold bit is the ${callsite}

我的SettingsHelper()测试:

My SettingsHelper() test:

ILogger logger = new Logger(typeof(SettingsHelper));
logger.Debug("A test from the same assembly as Logger()");

也不要忘记使用需要LogEventInfo()的重载

Don't forget also to use the overload that takes LogEventInfo()

_logger.Log(typeof(Logger), logEvent);

这篇关于NLog可以通过c#扩展方法保留呼叫站点信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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