-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
102 lines (87 loc) · 3.01 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// See https://aka.ms/new-console-template for more information
using System.Drawing;
using Serilog;
using Serilog.Events;
using System.Reflection;
using System.Runtime.CompilerServices;
using WebLogger;
using WebLogger.ConsoleApp.GeneratedCommands;
using WebLogger.ConsoleApp.GeneratedCommandStore;
using WebLogger.Utilities;
// Optionally create a collection of commands using the provided WebLoggerCommand class
var commands = new List<IWebLoggerCommand>()
{
new WebLoggerCommand(
(cmd, args) => CommandResponse.Success("EXAMPLE", $"{cmd} Received"),
"EXAMPLE",
"Simple example of console command",
"Parameter: NA"),
new WebLoggerCommand(
(cmd, args) => CommandResponse.Success("TEST", $"{cmd} Received"),
"TEST",
"Simple example of console command",
"Parameter: NA")
};
//Set the default logging level and pass it into the `ControlledBy` method
var logLevelCommand = new LoggerLevelCommand(LogEventLevel.Verbose);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(logLevelCommand.LoggingLevelSwitch)
.WriteTo.WebloggerSink(
options =>
{
options.Commands = commands;
options.Secured = false;
options.DestinationWebpageDirectory = "C:/Temp/WebLogger/Logger";
options.WebSocketTcpPort = 54321;
options.Colors.ProvideColors();
},
logger =>
{
logger.DiscoverCommands(Assembly.GetAssembly(typeof(Program)))
.DiscoverProvidedCommands();
////Register the command and now you can change the logging level.
//logger.RegisterCommand(logLevelCommand);
//logger.RegisterCommand(new AnotherCommand());
//logger.RegisterCommandStore(new RoomControlCommandStore());
//var roomCli = new RoomControlCommandStore();
//roomCli.RegisterCommands(logger);
})
.WriteTo.Console()
.CreateLogger();
CancellationTokenSource cts = new();
CancellationToken token = cts.Token;
try
{
Console.WriteLine($"Proceed to browser and open file: C:/Temp/WebLogger/Logger/index.html");
await DoWork();
}
catch (Exception e)
{
Log.Logger.Error(e, "Error in InitializeSystem: {0}", e.Message);
}
finally
{
cts.Cancel();
await Log.CloseAndFlushAsync();
}
async Task DoWork()
{
while (!token.IsCancellationRequested)
{
await Task.Delay(1000);
Log.Logger.Verbose("Verbose Log Level {name}, {something}", "Name", 2);
Log.Logger.Debug("Debug Log Level");
Log.Logger.Information("Information Log Level");
Log.Logger.Warning("Warning Log Level");
Log.Logger.Error("Error Log Level");
Log.Logger.Fatal(new Exception("Fake Fatal Exception", new Exception("Inner Exception", new Exception())), "Fatal Log Level");
try
{
throw new ArgumentException("Something cray");
}
catch (Exception e)
{
Log.Logger.Fatal(e, "First Exception");
}
}
}