Skip to content

Instantly share code, notes, and snippets.

@litetex
Last active May 13, 2025 09:53
Show Gist options
  • Select an option

  • Save litetex/b88fe0531e5acea82df1189643fb1f79 to your computer and use it in GitHub Desktop.

Select an option

Save litetex/b88fe0531e5acea82df1189643fb1f79 to your computer and use it in GitHub Desktop.

Revisions

  1. litetex revised this gist May 29, 2020. 2 changed files with 139 additions and 50 deletions.
    7 changes: 4 additions & 3 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    ## Serilog (C#): How to get the current MethodName, FileName/Path and LineNumber without reflection ##
    This is a simple setup for reflectionless logging with serilog using caller information (using a single static class).
    This is a simple setup for reflectionless logging with serilog using caller information (and a single static class).

    See also https://stackoverflow.com/a/46905798

    ### Log.cs ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the class below).


    This class is needed to detect from where the call is coming from; it uses [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) to speed up the program execution, because the attributes are resolved at compiletime.
    This class is required to detect where the call is coming from; it uses [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) to speed up the program execution, because the attributes are resolved at compile-time.



    @@ -45,4 +45,5 @@ Produces the following output:


    ### Changelog
    * 2020-03-21: Tuned docs a bit; updated and reformatted the Log.cs class
    * 2020-03-21: Tuned docs a bit; updated and reformatted the Log.cs class
    * 2020-05-29: Tuned docs a bit; Reformatted the Log.cs; References to CoreFramework.Logging
    182 changes: 135 additions & 47 deletions Log.cs
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    // MIT License
    // Copyright (c) 2020 litetex
    // See also https://github.com/litetex/CoreFramework/blob/develop/CoreFramework.Logging/Log.cs

    using System;
    using System.IO;
    using System.Runtime.CompilerServices;

    namespace <YourNamespaceHere>
    namespace CoreFramework
    {
    public static class Log
    {
    @@ -14,29 +15,36 @@ private static string FormatForException(this string message, Exception ex)
    return $"{message}: {(ex != null ? ex.ToString() : "")}";
    }

    private static string FormatForContext(this string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    private static string FormatForContext(
    this string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    var fileName = Path.GetFileNameWithoutExtension(sourceFilePath);
    var methodName = memberName;

    return $"{fileName} [{methodName}] {message}";
    }

    public static void Verbose(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Verbose(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Verbose(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Verbose(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Verbose(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Verbose(
    message
    @@ -45,19 +53,37 @@ public static void Verbose(string message, Exception ex, [CallerMemberName] stri
    );
    }

    public static void Debug(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Verbose(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Verbose(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Debug(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Debug(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Debug(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Debug(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Debug(
    message
    @@ -66,19 +92,37 @@ public static void Debug(string message, Exception ex, [CallerMemberName] string
    );
    }

    public static void Info(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Debug(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Debug(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Info(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Information(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Info(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Info(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Information(
    @@ -88,9 +132,23 @@ public static void Info(string message, Exception ex, [CallerMemberName] string
    );
    }

    public static void Warn(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Info(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Information(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Warn(string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Warning(
    @@ -99,9 +157,12 @@ public static void Warn(string message, [CallerMemberName] string memberName = "
    );
    }

    public static void Warn(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Warn(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Warning(
    @@ -111,9 +172,24 @@ public static void Warn(string message, Exception ex, [CallerMemberName] string
    );
    }

    public static void Error(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Warn(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Warning(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Error(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    @@ -122,9 +198,12 @@ public static void Error(string message, [CallerMemberName] string memberName =
    );
    }

    public static void Error(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Error(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    @@ -134,10 +213,11 @@ public static void Error(string message, Exception ex, [CallerMemberName] string
    );
    }

    public static void Error(Exception ex, [CallerMemberName]
    string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Error(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    @@ -146,9 +226,11 @@ public static void Error(Exception ex, [CallerMemberName]
    );
    }

    public static void Fatal(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Fatal(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    @@ -158,9 +240,12 @@ public static void Fatal(string message, [CallerMemberName] string memberName =
    );
    }

    public static void Fatal(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Fatal(
    string message,
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    @@ -171,9 +256,11 @@ public static void Fatal(string message, Exception ex, [CallerMemberName] string
    );
    }

    public static void Fatal(Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    public static void Fatal(
    Exception ex,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    @@ -189,3 +276,4 @@ private static void FatalAction()
    }
    }
    }

  2. litetex revised this gist Mar 21, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Description.md
    Original file line number Diff line number Diff line change
    @@ -44,5 +44,5 @@ Produces the following output:
    ``18:16:40,183 INFO TestClass [TestMethod] Some test``


    ## Update-Changelogs
    ### Changelog
    * 2020-03-21: Tuned docs a bit; updated and reformatted the Log.cs class
  3. litetex revised this gist Mar 21, 2020. 2 changed files with 48 additions and 40 deletions.
    8 changes: 5 additions & 3 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    ## Serilog (C#): How to get the current MethodName, FileName/Path and LineNumber without reflection ##
    Here is a simple setup for reflectionless logging with serilog using caller information.
    This is a simple setup for reflectionless logging with serilog using caller information (using a single static class).

    See also https://stackoverflow.com/a/46905798

    ### Log.cs ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).
    Create your own ``Log.cs`` in your Root-Namespace (you can use the class below).


    We need this class to detect from where the call is coming from. Using [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) speeds up the program execution, because the attributes are resolved at compiletime.
    This class is needed to detect from where the call is coming from; it uses [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) to speed up the program execution, because the attributes are resolved at compiletime.



    @@ -44,3 +44,5 @@ Produces the following output:
    ``18:16:40,183 INFO TestClass [TestMethod] Some test``


    ## Update-Changelogs
    * 2020-03-21: Tuned docs a bit; updated and reformatted the Log.cs class
    80 changes: 43 additions & 37 deletions Log.cs
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,11 @@
    // MIT License
    // Copyright (c) 2019 litetex
    // Copyright (c) 2020 litetex

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;

    namespace <YourNameSpaceHere>
    namespace <YourNamespaceHere>
    {
    public static class Log
    {
    @@ -34,7 +30,7 @@ public static void Verbose(string message, [CallerMemberName] string memberName
    {
    Serilog.Log.Verbose(
    message
    .FormatForContext(memberName,sourceFilePath,sourceLineNumber)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    @@ -44,17 +40,18 @@ public static void Verbose(string message, Exception ex, [CallerMemberName] stri
    {
    Serilog.Log.Verbose(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Debug(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Debug(message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    Serilog.Log.Debug(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    @@ -64,8 +61,8 @@ public static void Debug(string message, Exception ex, [CallerMemberName] string
    {
    Serilog.Log.Debug(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    @@ -75,106 +72,115 @@ public static void Info(string message, [CallerMemberName] string memberName = "
    {
    Serilog.Log.Information(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Info(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Information(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Warn(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Warning(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Warn(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Warning(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Error(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Error(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Error(Exception ex, [CallerMemberName]
    string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {

    Serilog.Log.Error(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Fatal(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Fatal(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Fatal(Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    (ex == null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    private static void FatalAction()
  4. litetex revised this gist Oct 11, 2019. 1 changed file with 106 additions and 27 deletions.
    133 changes: 106 additions & 27 deletions Log.cs
    Original file line number Diff line number Diff line change
    @@ -13,94 +13,173 @@ namespace <YourNameSpaceHere>
    {
    public static class Log
    {
    private static string FormatForException(this string message, Exception ex)
    {
    return $"{message}: {(ex != null ? ex.ToString() : "")}";
    }

    private static void SetContext([CallerMemberName] string memberName = "",
    private static string FormatForContext(this string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Logger = Serilog.Log.Logger
    .ForContext("MemberName", memberName)
    .ForContext("FilePath", sourceFilePath)
    .ForContext("FileName", Path.GetFileNameWithoutExtension(sourceFilePath))
    .ForContext("LineNumber", sourceLineNumber);
    var fileName = Path.GetFileNameWithoutExtension(sourceFilePath);
    var methodName = memberName;

    return $"{fileName} [{methodName}] {message}";
    }

    private static string FormatExceptionAndMessage(string message, Exception ex)
    public static void Verbose(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    return $"{message}: {(ex != null ? ex.ToString() : "")}";
    Serilog.Log.Verbose(
    message
    .FormatForContext(memberName,sourceFilePath,sourceLineNumber)
    );
    }

    public static void Verbose(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Verbose(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Debug(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Debug(message);
    Serilog.Log.Debug(message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Debug(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Debug(FormatExceptionAndMessage(message, ex));
    Serilog.Log.Debug(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Info(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Information(message);
    Serilog.Log.Information(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber)
    );
    }

    public static void Info(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Information(FormatExceptionAndMessage(message, ex));

    Serilog.Log.Information(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Warn(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Warning(message);

    Serilog.Log.Warning(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Warn(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Warning(FormatExceptionAndMessage(message, ex));

    Serilog.Log.Warning(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Error(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(message);

    Serilog.Log.Error(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Error(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(FormatExceptionAndMessage(message, ex));

    Serilog.Log.Error(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Error(Exception ex, [CallerMemberName]
    string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(ex == null ? ex.ToString() : "");

    Serilog.Log.Error(
    (ex != null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Fatal(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    message
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Fatal(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    message
    .FormatForException(ex)
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    public static void Fatal(Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    FatalAction();

    Serilog.Log.Error(
    (ex == null ? ex.ToString() : "")
    .FormatForContext(memberName, sourceFilePath, sourceLineNumber));
    }

    private static void FatalAction()
    {
    Environment.ExitCode = -1;
    }
    }
    }
    }
  5. litetex revised this gist Jun 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Description.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Serilog (C#): How to get current MethodName, FileName/Path and LineNumber (without reflection) ##
    ## Serilog (C#): How to get the current MethodName, FileName/Path and LineNumber without reflection ##
    Here is a simple setup for reflectionless logging with serilog using caller information.

    See also https://stackoverflow.com/a/46905798
  6. litetex revised this gist Jun 14, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    ## Serilog (C#): How to get current MethodName, FileName/Path and LineNumber (without reflection) ##
    Here is a simple setup for reflectionless logging with serilog using caller information.

    See also https://stackoverflow.com/a/46905798

    ### Log.cs ###
  7. litetex revised this gist Jun 14, 2019. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Serilog (C#): How to get current MethodName, FileName/Path and LineNumber (without reflection) #
    ## Setup ##
    ## Serilog (C#): How to get current MethodName, FileName/Path and LineNumber (without reflection) ##
    Here is a simple setup for reflectionless logging with serilog using caller information.
    See also https://stackoverflow.com/a/46905798

    ### Log.cs ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).

  8. litetex revised this gist Jun 14, 2019. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # Setup #
    ## Log.cs ##
    # Serilog (C#): How to get current MethodName, FileName/Path and LineNumber (without reflection) #
    ## Setup ##
    ### Log.cs ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).


    @@ -11,7 +12,7 @@ You can now also remove all ``using Serilog;``-imports - if you have any - in th



    ## Serilog-Output Template ##
    ### Serilog-Output Template ###
    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:


    @@ -20,7 +21,7 @@ _:warning: Note: For more performance you can remove/uncomment unused properties



    ### Example ###
    ### Final Example ###
    OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`

    ```csharp
  9. litetex revised this gist Jun 14, 2019. 1 changed file with 17 additions and 7 deletions.
    24 changes: 17 additions & 7 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,17 @@
    ### Setup ###
    #### Log.cs ####
    # Setup #
    ## Log.cs ##
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).


    We need this class to detect from where in the program the call is coming from.

    Using [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) speeds up the program execution, because the attributes are resolved at compiler time.
    We need this class to detect from where the call is coming from. Using [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) speeds up the program execution, because the attributes are resolved at compiletime.



    You can now also remove all ``using Serilog;``-imports - if you have any - in the classes where you want to log, because the ``Log.cs`` is everywhere in the namespace visble and easy accessible.



    #### Serilog-Output Template ###
    ## Serilog-Output Template ##
    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:


    @@ -22,9 +20,21 @@ _:warning: Note: For more performance you can remove/uncomment unused properties



    ##### Example #####
    ### Example ###
    OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`

    ```csharp
    namespace Demo
    {
    public class TestClass
    {
    public void TestMethod()
    {
    Log.Info("Some test");
    }
    }
    }
    ```

    Produces the following output:
    ``18:16:40,183 INFO TestClass [TestMethod] Some test``
  10. litetex revised this gist Jun 14, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Log.cs
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ private static void SetContext([CallerMemberName] string memberName = "",
    .ForContext("LineNumber", sourceLineNumber);
    }

    private static string GetLogString(string message, Exception ex)
    private static string FormatExceptionAndMessage(string message, Exception ex)
    {
    return $"{message}: {(ex != null ? ex.ToString() : "")}";
    }
    @@ -43,7 +43,7 @@ public static void Debug(string message, Exception ex, [CallerMemberName] string
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Debug(GetLogString(message, ex));
    Serilog.Log.Debug(FormatExceptionAndMessage(message, ex));
    }

    public static void Info(string message, [CallerMemberName] string memberName = "",
    @@ -59,7 +59,7 @@ public static void Info(string message, Exception ex, [CallerMemberName] string
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Information(GetLogString(message, ex));
    Serilog.Log.Information(FormatExceptionAndMessage(message, ex));
    }

    public static void Warn(string message, [CallerMemberName] string memberName = "",
    @@ -75,7 +75,7 @@ public static void Warn(string message, Exception ex, [CallerMemberName] string
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Warning(GetLogString(message, ex));
    Serilog.Log.Warning(FormatExceptionAndMessage(message, ex));
    }

    public static void Error(string message, [CallerMemberName] string memberName = "",
    @@ -91,7 +91,7 @@ public static void Error(string message, Exception ex, [CallerMemberName] string
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(GetLogString(message, ex));
    Serilog.Log.Error(FormatExceptionAndMessage(message, ex));
    }

    public static void Error(Exception ex, [CallerMemberName]
  11. litetex revised this gist Jun 14, 2019. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,30 @@
    #### Log.cs ####
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).


    We need this class to detect from where in the program the call is coming from.

    Using [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) speeds up the program execution, because the attributes are resolved at compiler time.



    You can now also remove all ``using Serilog;``-imports - if you have any - in the classes where you want to log, because the ``Log.cs`` is everywhere in the namespace visble and easy accessible.



    #### Serilog-Output Template ###
    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:



    _:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``_



    ##### Example #####
    OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`


    Produces the following output:
    ``18:16:40,183 INFO TestClass [TestMethod] Some test``

  12. litetex revised this gist Jun 14, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion Description.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,11 @@
    #### Log.cs ####
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).

    You can now also remove all ``using Serilog;``-imports in the classes where you want to log, because the class is everywhere in the namespace visble and easy accessible.
    We need this class to detect from where in the program the call is coming from.
    Using [Caller-Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) speeds up the program execution, because the attributes are resolved at compiler time.


    You can now also remove all ``using Serilog;``-imports - if you have any - in the classes where you want to log, because the ``Log.cs`` is everywhere in the namespace visble and easy accessible.


    #### Serilog-Output Template ###
  13. litetex revised this gist Jun 14, 2019. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Description.md
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,21 @@
    ### Setup ###
    #### Log.cs ####
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).

    You can now also remove all ``using Serilog;``-imports in the classes where you want to log, because the class is everywhere in the namespace visble and easy accessible.


    #### Serilog-Output Template ###
    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:


    _:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``_


    ##### Example #####
    OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`

    Produces the following output:
    ``18:16:40,183 INFO LoggerInitializer [InitLogger] Shutting down logger; Flushing data...``
    ``18:16:40,183 INFO TestClass [TestMethod] Some test``


  14. litetex revised this gist Jun 14, 2019. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    ### Setup ###
    #### Log.cs ####
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).
    You can now also remove all ``using Serilog;``-imports in the classes where you want to log, because the class is everywhere in the namespace visble and easy accessible.

    #### Serilog-Output Template ###
    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:
    e.g. `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`
    which produces:

    _:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``_

    ##### Example #####
    OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`
    Produces the following output:
    ``18:16:40,183 INFO LoggerInitializer [InitLogger] Shutting down logger; Flushing data...``


    _:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``_
  15. litetex revised this gist Jun 14, 2019. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@

    ### Setup ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).
    You can now also remove all ``using Serilog;``-imports in the classes where you want to log, because the class is everywhere in the namespace visble and easy accessible.

    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:
    e.g. `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`

    this produces:
    which produces:
    ``18:16:40,183 INFO LoggerInitializer [InitLogger] Shutting down logger; Flushing data...``

    <small>:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``</small>

    _:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``_
  16. litetex revised this gist Jun 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Description.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,4 @@ e.g. `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}
    this produces:
    ``18:16:40,183 INFO LoggerInitializer [InitLogger] Shutting down logger; Flushing data...``

    :warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``
    <small>:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``</small>
  17. litetex created this gist Jun 14, 2019.
    11 changes: 11 additions & 0 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@

    ### Setup ###
    Create your own ``Log.cs`` in your Root-Namespace (you can use the below mentioned class).

    Now you can use the added properties (here: ``MemberName``, ``FilePath``, ``FileName``, ``LineNumber``) in your outputTemplate:
    e.g. `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}`

    this produces:
    ``18:16:40,183 INFO LoggerInitializer [InitLogger] Shutting down logger; Flushing data...``

    :warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``
    106 changes: 106 additions & 0 deletions Log.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    // MIT License
    // Copyright (c) 2019 litetex

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;

    namespace <YourNameSpaceHere>
    {
    public static class Log
    {

    private static void SetContext([CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    Serilog.Log.Logger = Serilog.Log.Logger
    .ForContext("MemberName", memberName)
    .ForContext("FilePath", sourceFilePath)
    .ForContext("FileName", Path.GetFileNameWithoutExtension(sourceFilePath))
    .ForContext("LineNumber", sourceLineNumber);
    }

    private static string GetLogString(string message, Exception ex)
    {
    return $"{message}: {(ex != null ? ex.ToString() : "")}";
    }

    public static void Debug(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Debug(message);
    }

    public static void Debug(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Debug(GetLogString(message, ex));
    }

    public static void Info(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Information(message);
    }

    public static void Info(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Information(GetLogString(message, ex));
    }

    public static void Warn(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Warning(message);
    }

    public static void Warn(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Warning(GetLogString(message, ex));
    }

    public static void Error(string message, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(message);
    }

    public static void Error(string message, Exception ex, [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(GetLogString(message, ex));
    }

    public static void Error(Exception ex, [CallerMemberName]
    string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
    SetContext(memberName, sourceFilePath, sourceLineNumber);
    Serilog.Log.Error(ex == null ? ex.ToString() : "");
    }
    }
    }