2

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?

1 Answer 1

8

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.