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 characters
| // Explicitly including the class | |
| Log.Logger.ForContext<MainWindow>().Information("This log entry includes the class name as context"); | |
| // More generic getting the type of the class we are in | |
| Log.Logger.ForContext(this.GetType()).Information("This log entry includes the class name as context"); |
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 characters
| var dynamicObject = new | |
| { | |
| OperatingSystem = "Windows", | |
| Username = "TestUser", | |
| OneMoreRandomProperty = "Random Property Value " | |
| }; | |
| using (LogContext.PushProperty("DynamicObject", dynamicObject, destructureObjects: true)) | |
| { | |
| Log.Logger.Information("This log entry includes a dynamic object"); |
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 characters
| // Declare Person class | |
| public class Person | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| } | |
| // Instantiate a Person | |
| var person = new Person() | |
| { |
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 characters
| // Add log event with a contextual property | |
| using (LogContext.PushProperty("User", "Test User")) | |
| { | |
| Log.Logger.Information("This log entry includes a contextual property"); | |
| } | |
| // Add log event without contextual properties | |
| Log.Logger.Information("This log entry does not include any contextual property"); | |
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 characters
| // Create Logger with Context | |
| Log.Logger = new LoggerConfiguration() | |
| .Enrich.FromLogContext() // Enrich with context when available | |
| .WriteTo.Seq("http://localhost:5341") | |
| .CreateLogger(); |
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 characters
| Log.Logger.Error(new InvalidOperationException("Something went wrong"), | |
| "This exception occured in the {ApplicationComponent}", "UI"); |
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 characters
| // Declare Person class | |
| public class Person | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| } | |
| // Instantiate a Person | |
| var person = new Person() | |
| { |
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 characters
| // Define two input parameters | |
| string fName = "Dimitris"; | |
| string lName = "Paxinos"; | |
| // Include them in a way similar to the String.Format way. | |
| Log.Logger.Information("My name is {FirstName} {LastName}", fName, lName); |
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 characters
| // Publish a log event of any of the available six levels | |
| Log.Logger.Verbose("Verbose"); | |
| Log.Logger.Debug("Debug"); | |
| Log.Logger.Information("Information"); | |
| Log.Logger.Warning("Warning"); | |
| Log.Logger.Error("Error"); | |
| Log.Logger.Fatal("Fatal"); |
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 characters
| // Create an Logger | |
| Log.Logger = new LoggerConfiguration() | |
| .WriteTo.Seq("http://localhost:5341") | |
| .CreateLogger(); |