【问题标题】:Nlog logging methodsNlog 日志记录方法
【发布时间】:2013-07-21 00:24:17
【问题描述】:

我正在做的一个小型网站开发项目正处于 alpha 阶段,并决定使用 NLog 作为我的日志记录解决方案。

到目前为止,我的解决方案是在没有记录的情况下开发的。我现在正在添加日志记录。

一个例子:

private static Logger logger = LogManager.GetCurrentClassLogger();

public int SaveProject(ProjectDto project)
{
    logger.Trace("SaveProject ({0}) : {1}", project.Id, _userId);
    return _pb.SaveProject(project);
}

“GetCurrentClassLogger”方法很棒,因为它现在知道我在哪个类。

但是有没有办法报告方法名称,而不是我的做法?在上面的示例中,您可以看到我需要在消息中添加“SaveProject”。有没有办法自动得到这个?还是我需要将其添加到每个方法记录调用中?

【问题讨论】:

    标签: c# logging nlog


    【解决方案1】:

    是的,请参阅callsite layout renderer。你把它放在你的布局配置中。例如:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        <targets>
            <target name="console" xsi:type="ColoredConsole" layout="${callsite:className=false:includeSourcePath=false:methodName=true} ${message}"/>
        </targets>
    
        <rules>
            <logger name="*" minlevel="Trace" writeTo="console" />
        </rules>
    </nlog>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-29
      • 2018-03-24
      • 2017-05-22
      • 2023-03-11
      • 1970-01-01
      • 2014-10-04
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多