Nlog日志记录方法

Cra*_*aig 2 c# logging nlog

我正处于我正在进行的小型网站开发项目的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);
}
Run Code Online (Sandbox Code Playgroud)

'GetCurrentClassLogger'方法很棒,因为它现在知道我在哪个类.

但有没有办法报告方法名称,而不是我如何做?在上面的示例中,您可以看到我需要在消息中添加"SaveProject".有没有办法自动获得这个?或者我是否需要将此添加到每个方法记录调用?

Ken*_*art 8

是的,请参阅callsite布局渲染器.你把它放在你的布局配置中.例如:

<?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>
Run Code Online (Sandbox Code Playgroud)