-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
55 lines (42 loc) · 2.08 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// This project uses new C# templates to generate top-level statements
// See https://aka.ms/new-console-template for more information
/**
* This project showcases logging to Better Stack
*/
using Serilog;
// Create logger
Log.Logger = new LoggerConfiguration()
.WriteTo.BetterStack(
sourceToken: "<source_token>",
betterStackEndpoint: "<ingesting_host>"
)
.WriteTo.Console()
.MinimumLevel.Debug()
.CreateLogger();
// Enables Serilog internal logs to help troubleshoot logging
Serilog.Debugging.SelfLog.Enable(Console.Error);
// Following code showcases 6 Serilog's default log levels
// Additionally, it also show how to structure logs to add additional data
// Trace the code or add detailed debugging info using the Verbose() method
// Due to MinimumLevel setting, this log event will be skipped
Log.Verbose("Tracing the code!");
// Send debug messages using the Debug() method
Log.Debug("Debugging is hard, but can be easier with Better Stack!");
// Send informative messages about application progress using the Info() method
// All of the properties that you pass to the log will be stored in a structured
// form in the context section of the logged event
Log.Information("User {User} - {UserId} just ordered item {Item}", "Josh", 95845, 75423);
// Use context to tag events with additional data
var loggerWithContext = Log.ForContext<Program>()
.ForContext("ProcessId", 123)
.ForContext("UserEmail", "[email protected]");
// Report non-critical issues using the Warn() method
loggerWithContext.Warning("Something is happening!");
// Send message about serious problems using the Error() method
loggerWithContext.Error("Error occurred! And it's not good.");
// Report fatal errors that caused application to crash using the Fatal() method
loggerWithContext.Fatal("Application crashed! Needs to be fixed ASAP!");
// For logs to be send to Better Stack reliably, Serilog must be properly closed
// see https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers
Log.CloseAndFlush();
Console.WriteLine("All done! Now, you can check Better Stack to see your logs");