-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Managed Core Logging & Plugin Logging (#102)
- Loading branch information
1 parent
6147739
commit bb5fb5d
Showing
17 changed files
with
243 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
29 changes: 29 additions & 0 deletions
29
managed/CounterStrikeSharp.API/Core/Logging/CoreLogging.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.IO; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using ILogger = Microsoft.Extensions.Logging.ILogger; | ||
|
||
namespace CounterStrikeSharp.API.Core.Logging; | ||
|
||
public static class CoreLogging | ||
{ | ||
public static ILoggerFactory Factory { get; } | ||
|
||
static CoreLogging() | ||
{ | ||
var logger = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.Enrich.With<SourceContextEnricher>() | ||
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u4}] (cssharp:{SourceContext}) {Message:lj}{NewLine}{Exception}") | ||
.WriteTo.File(Path.Join(new[] {GlobalContext.RootDirectory, "logs", $"log-cssharp.txt"}), rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] (cssharp:{SourceContext}) {Message:lj}{NewLine}{Exception}") | ||
.WriteTo.File(Path.Join(new[] {GlobalContext.RootDirectory, "logs", $"log-all.txt"}), rollingInterval: RollingInterval.Day, shared: true, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] (cssharp:{SourceContext}) {Message:lj}{NewLine}{Exception}") | ||
.CreateLogger(); | ||
|
||
Factory = | ||
LoggerFactory.Create(builder => | ||
{ | ||
builder.AddSerilog(logger); | ||
}); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
managed/CounterStrikeSharp.API/Core/Logging/PluginLogging.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.IO; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using ILogger = Microsoft.Extensions.Logging.ILogger; | ||
|
||
namespace CounterStrikeSharp.API.Core.Logging; | ||
|
||
public class PluginLogging | ||
{ | ||
/// <summary> | ||
/// Creates a logger scoped to a specific plugin | ||
/// <remarks>Eventually this should probably come from a service collection</remarks> | ||
/// </summary> | ||
public static ILogger CreatePluginLogger(PluginContext pluginContext) | ||
{ | ||
var logger = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.Enrich.With(new PluginNameEnricher(pluginContext)) | ||
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u4}] (plugin:{PluginName}) {Message:lj}{NewLine}{Exception}") | ||
.WriteTo.File(Path.Join(new[] {GlobalContext.RootDirectory, "logs", $"log-{pluginContext.PluginType.Name}.txt"}), rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] plugin:{PluginName} {Message:lj}{NewLine}{Exception}") | ||
.WriteTo.File(Path.Join(new[] {GlobalContext.RootDirectory, "logs", $"log-all.txt"}), rollingInterval: RollingInterval.Day, shared: true, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] plugin:{PluginName} {Message:lj}{NewLine}{Exception}") | ||
.CreateLogger(); | ||
|
||
using ILoggerFactory loggerFactory = | ||
LoggerFactory.Create(builder => | ||
{ | ||
builder.AddSerilog(logger); | ||
}); | ||
|
||
return loggerFactory.CreateLogger(pluginContext.PluginType); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
managed/CounterStrikeSharp.API/Core/Logging/PluginNameEnricher.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace CounterStrikeSharp.API.Core.Logging; | ||
|
||
public class PluginNameEnricher : ILogEventEnricher | ||
{ | ||
public const string PropertyName = "PluginName"; | ||
|
||
public PluginNameEnricher(PluginContext pluginContext) | ||
{ | ||
Context = pluginContext; | ||
} | ||
|
||
public PluginContext Context { get; } | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
var property = propertyFactory.CreateProperty(PropertyName, Context.PluginType.Name); | ||
logEvent.AddPropertyIfAbsent(property); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
managed/CounterStrikeSharp.API/Core/Logging/SourceContextEnricher.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Linq; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace CounterStrikeSharp.API.Core.Logging; | ||
|
||
public class SourceContextEnricher : ILogEventEnricher | ||
{ | ||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (logEvent.Properties.TryGetValue("SourceContext", out var property)) | ||
{ | ||
var scalarValue = property as ScalarValue; | ||
var value = scalarValue?.Value as string; | ||
|
||
if (value?.StartsWith("CounterStrikeSharp") ?? false) | ||
{ | ||
var lastElement = value.Split(".").LastOrDefault(); | ||
if (!string.IsNullOrWhiteSpace(lastElement)) | ||
{ | ||
logEvent.AddOrUpdateProperty(new LogEventProperty("SourceContext", new ScalarValue(lastElement))); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.