-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32334a0
commit a704612
Showing
6 changed files
with
92 additions
and
18 deletions.
There are no files selected for viewing
Submodule CUE4Parse
updated
19 files
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace FModel.Framework; | ||
|
||
public abstract class SerilogEnricher : ILogEventEnricher | ||
{ | ||
public abstract void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory); | ||
|
||
protected bool TryGetCaller(out MethodBase method) | ||
{ | ||
method = null; | ||
|
||
var serilogAssembly = typeof(Serilog.Log).Assembly; | ||
var stack = new StackTrace(3); | ||
|
||
foreach (var frame in stack.GetFrames()) | ||
{ | ||
var m = frame.GetMethod(); | ||
if (m?.DeclaringType is null) continue; | ||
|
||
if (m.DeclaringType.Assembly != serilogAssembly) | ||
{ | ||
method = m; | ||
break; | ||
} | ||
} | ||
|
||
return method != null; | ||
} | ||
} | ||
|
||
public class SourceEnricher : SerilogEnricher | ||
{ | ||
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
var source = "N/A"; | ||
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContext)) | ||
{ | ||
source = sourceContext.ToString()[1..^1]; | ||
} | ||
else if (TryGetCaller(out var method)) | ||
{ | ||
source = method.DeclaringType?.Namespace; | ||
} | ||
|
||
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", source.Split('.')[0])); | ||
} | ||
} | ||
|
||
public class CallerEnricher : SerilogEnricher | ||
{ | ||
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (TryGetCaller(out var method)) | ||
{ | ||
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", $"{method.DeclaringType?.FullName}.{method.Name}")); | ||
} | ||
} | ||
} |
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