Last active
May 13, 2025 09:53
-
-
Save litetex/b88fe0531e5acea82df1189643fb1f79 to your computer and use it in GitHub Desktop.
Revisions
-
litetex revised this gist
May 29, 2020 . 2 changed files with 139 additions and 50 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 (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 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-05-29: Tuned docs a bit; Reformatted the Log.cs; References to CoreFramework.Logging This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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) { 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) { 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 @@ -45,19 +53,37 @@ public static void Verbose(string message, Exception ex, [CallerMemberName] stri ); } 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) { Serilog.Log.Debug( message @@ -66,19 +92,37 @@ public static void Debug(string message, Exception ex, [CallerMemberName] string ); } 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) { Serilog.Log.Information( @@ -88,9 +132,23 @@ public static void Info(string message, Exception ex, [CallerMemberName] string ); } 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) { Serilog.Log.Warning( @@ -111,9 +172,24 @@ public static void Warn(string message, Exception ex, [CallerMemberName] string ); } 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) { 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) { 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) { 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) { 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) { FatalAction(); @@ -189,3 +276,4 @@ private static void FatalAction() } } } -
litetex revised this gist
Mar 21, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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`` ### Changelog * 2020-03-21: Tuned docs a bit; updated and reformatted the Log.cs class -
litetex revised this gist
Mar 21, 2020 . 2 changed files with 48 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). 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. @@ -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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,11 @@ // MIT License // Copyright (c) 2020 litetex using System; using System.IO; using System.Runtime.CompilerServices; 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) ); } @@ -44,17 +40,18 @@ public static void Verbose(string message, Exception ex, [CallerMemberName] stri { 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) { 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) ); } @@ -75,106 +72,115 @@ public static void Info(string message, [CallerMemberName] string memberName = " { 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) { 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) { 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) { 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) { 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) { 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) { 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() -
litetex revised this gist
Oct 11, 2019 . 1 changed file with 106 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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; } } } -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ## 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 -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ### -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ## 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). -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) # ## 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 ### 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 ### Final Example ### OutputTemplate: `{Timestamp:HH:mm:ss,fff} {Level:u3} {FileName} [{MemberName}] {Message:lj}{NewLine}{Exception}` ```csharp -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 17 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,17 @@ # 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 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 ## 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 ### 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`` -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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(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(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(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(FormatExceptionAndMessage(message, ex)); } public static void Error(Exception ex, [CallerMemberName] -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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`` -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). 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 ### -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 TestClass [TestMethod] Some test`` -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: _: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...`` -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,10 @@ 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}` which 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(...)``_ -
litetex revised this gist
Jun 14, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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...`` <small>:warning: Note: For more performance you can remove/uncomment unused properties, here e.g. ``FilePath`` and ``LineNumber`` in ``SetContext(...)``</small> -
litetex created this gist
Jun 14, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(...)`` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() : ""); } } }