How to retain callsite information when wrapping NLog

后端 未结 8 2171
感情败类
感情败类 2020-11-29 02:33

I have a class that wraps NLog (called NLogger). My logs are saved to my database. The thing I\'m having a problem with is how do I show where the logging occured. I have th

8条回答
  •  囚心锁ツ
    2020-11-29 02:50

    There is an easy way to achieve this. Just add these attributes to your log wrapper method signatures:

    void Log(LogSeverity severity, string message, [CallerFilePath] string fileName = null, [CallerMemberName] string member = null, [CallerLineNumber] int? lineNumber = null);
    

    and pass these to the wrapped NLog methods.

    See https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerfilepathattribute?view=netframework-4.7.2 for more info on System.Runtime.CompilerServices attibutes in .NET.

提交回复
热议问题