-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHosting.cs
More file actions
51 lines (44 loc) · 2.1 KB
/
Copy pathHosting.cs
File metadata and controls
51 lines (44 loc) · 2.1 KB
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
using Microsoft.Extensions.Logging;
using Serilog;
using Terminal.Gui;
using Terminal.Gui.App;
using Terminal.Gui.Tracing;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Ted;
/// <summary>
/// Process-wide hosting concerns for the <c>ted</c> demo: Serilog → MEL → Terminal.Gui's
/// <see cref="Logging.Logger" />, plus <see cref="Trace.EnabledCategories" />.
/// Extracted from <c>Program.cs</c> so tests can call it and assert side effects.
/// </summary>
public static class Hosting
{
/// <summary>Default trace categories for ted. Useful enough to debug menu/key/mouse flow without flooding the log.</summary>
public const TraceCategory DefaultTraceCategories =
TraceCategory.Command | TraceCategory.Keyboard | TraceCategory.Mouse;
/// <summary>Default log file path. Daily rolling.</summary>
public const string DefaultLogPath = "logs/ted.log";
/// <summary>
/// Configures Serilog → Microsoft.Extensions.Logging → <see cref="Logging.Logger" />. Returns the
/// <see cref="ILogger" /> assigned to <see cref="Logging.Logger" /> so callers can also use it directly.
/// </summary>
public static ILogger ConfigureLogging (string? logPath = null)
{
Log.Logger = new LoggerConfiguration ()
.MinimumLevel.Verbose ()
.Enrich.FromLogContext ()
.WriteTo.File (logPath ?? DefaultLogPath,
rollingInterval: RollingInterval.Day,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger ();
ILoggerFactory factory =
LoggerFactory.Create (b => b.AddSerilog (dispose: true).SetMinimumLevel (LogLevel.Trace));
ILogger logger = factory.CreateLogger ("ted");
Logging.Logger = logger;
return logger;
}
/// <summary>Sets <see cref="Trace.EnabledCategories" />. Defaults to <see cref="DefaultTraceCategories" />.</summary>
public static void EnableTracing (TraceCategory categories = DefaultTraceCategories)
{
Trace.EnabledCategories = categories;
}
}