Skip to content

Instantly share code, notes, and snippets.

@wgross
Created February 19, 2017 02:33
Show Gist options
  • Save wgross/bea2209ee1395b60df2e962e93a041a4 to your computer and use it in GitHub Desktop.
Save wgross/bea2209ee1395b60df2e962e93a041a4 to your computer and use it in GitHub Desktop.
Adding Callerinfo to Serilog Context
public static class LoggerExtensions
{
public static ILogger WithCallerInfo(this ILogger logger,
[CallerMemberName]string callerMemberName = null,
[CallerFilePath]string callerFilePath = null,
[CallerLineNumber]int callerLineNumber = 0)
{
return logger
// substituting 'Caller' with 'Source' because 'ForContext<T>()' adds property 'SourceContext'
.ForContext("SourceMemberName", callerMemberName)
.ForContext("SourceFilePath", callerFilePath)
.ForContext("SourceLineNumber", callerLineNumber);
}
}
// see also: https://gist.github.com/cbdabner/b2c921a3a6da05cdb946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment