Craig
Craig

Reputation: 18684

Nlog logging methods

I am in the very alpha stage of a small website development project I am doing, and have decided to use NLog as my logging solution.

My solution is developed so far without logging. I am adding in the logging now.

An example:

private static Logger logger = LogManager.GetCurrentClassLogger();

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

The 'GetCurrentClassLogger' method is great in that it now knows which class I am in.

But is there a way to report the Method name, instead of how I am doing it? In the example above, you can see I need to add "SaveProject" to the message. Is there a way to automatically get this? Or do I need to add this to every method logging call?

Upvotes: 2

Views: 6652

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178630

Yes, see the callsite layout renderer. You put it in your layout configuration. eg:

<?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>

Upvotes: 8

Related Questions