Skip to content

Commit a704612

Browse files
committed
SerilogEnricher
1 parent 32334a0 commit a704612

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

FModel/App.xaml.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,20 @@ protected override void OnStartup(StartupEventArgs e)
104104
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Logs"));
105105
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, ".data"));
106106

107+
const string template = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Enriched}: {Message:lj}{NewLine}{Exception}";
108+
Log.Logger = new LoggerConfiguration()
107109
#if DEBUG
108-
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File(
109-
Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt"),
110-
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger();
110+
.Enrich.With<SourceEnricher>()
111+
.MinimumLevel.Verbose()
112+
.WriteTo.Console(outputTemplate: template, theme: AnsiConsoleTheme.Literate)
113+
.WriteTo.File(outputTemplate: template,
114+
path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt"))
111115
#else
112-
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File(
113-
Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"),
114-
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger();
116+
.Enrich.With<CallerEnricher>()
117+
.WriteTo.File(outputTemplate: template,
118+
path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"))
115119
#endif
120+
.CreateLogger();
116121

117122
Log.Information("Version {Version} ({CommitId})", Constants.APP_VERSION, Constants.APP_COMMIT_ID);
118123
Log.Information("{OS}", GetOperatingSystemProductName());
@@ -183,4 +188,4 @@ public static string GetRegistryValue(string path, string name = null, RegistryH
183188
return rk.GetValue(name, null) as string;
184189
return string.Empty;
185190
}
186-
}
191+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Reflection;
4+
using Serilog.Core;
5+
using Serilog.Events;
6+
7+
namespace FModel.Framework;
8+
9+
public abstract class SerilogEnricher : ILogEventEnricher
10+
{
11+
public abstract void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory);
12+
13+
protected bool TryGetCaller(out MethodBase method)
14+
{
15+
method = null;
16+
17+
var serilogAssembly = typeof(Serilog.Log).Assembly;
18+
var stack = new StackTrace(3);
19+
20+
foreach (var frame in stack.GetFrames())
21+
{
22+
var m = frame.GetMethod();
23+
if (m?.DeclaringType is null) continue;
24+
25+
if (m.DeclaringType.Assembly != serilogAssembly)
26+
{
27+
method = m;
28+
break;
29+
}
30+
}
31+
32+
return method != null;
33+
}
34+
}
35+
36+
public class SourceEnricher : SerilogEnricher
37+
{
38+
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
39+
{
40+
var source = "N/A";
41+
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContext))
42+
{
43+
source = sourceContext.ToString()[1..^1];
44+
}
45+
else if (TryGetCaller(out var method))
46+
{
47+
source = method.DeclaringType?.Namespace;
48+
}
49+
50+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", source.Split('.')[0]));
51+
}
52+
}
53+
54+
public class CallerEnricher : SerilogEnricher
55+
{
56+
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
57+
{
58+
if (TryGetCaller(out var method))
59+
{
60+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", $"{method.DeclaringType?.FullName}.{method.Name}"));
61+
}
62+
}
63+
}

FModel/MainWindow.xaml.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ await Task.WhenAll(
8585
#if DEBUG
8686
// await _threadWorkerView.Begin(cancellationToken =>
8787
// _applicationView.CUE4Parse.Extract(cancellationToken,
88-
// "FortniteGame/Content/Athena/Apollo/Maps/UI/Apollo_Terrain_Minimap.uasset"));
88+
// "MyProject/Content/FirstPerson/Meshes/FirstPersonProjectileMesh.uasset"));
8989
// await _threadWorkerView.Begin(cancellationToken =>
9090
// _applicationView.CUE4Parse.Extract(cancellationToken,
91-
// "FortniteGame/Content/Environments/Helios/Props/GlacierHotel/GlacierHotel_Globe_A/Meshes/SM_GlacierHotel_Globe_A.uasset"));
91+
// "RED/Content/Chara/ABA/Costume01/Animation/Charaselect/body/stand_body01.uasset"));
9292
#endif
9393
}
9494

@@ -208,7 +208,7 @@ private async void OnFolderTextureClick(object sender, RoutedEventArgs e)
208208
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.TextureFolder(cancellationToken, folder); });
209209
FLogger.Append(ELog.Information, () =>
210210
{
211-
FLogger.Text("Successfully saved ", Constants.WHITE);
211+
FLogger.Text("Successfully saved textures from ", Constants.WHITE);
212212
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.TextureDirectory, true);
213213
});
214214
}
@@ -219,6 +219,11 @@ private async void OnFolderModelClick(object sender, RoutedEventArgs e)
219219
if (AssetsFolderName.SelectedItem is TreeItem folder)
220220
{
221221
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.ModelFolder(cancellationToken, folder); });
222+
FLogger.Append(ELog.Information, () =>
223+
{
224+
FLogger.Text("Successfully saved models from ", Constants.WHITE);
225+
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true);
226+
});
222227
}
223228
}
224229

@@ -227,6 +232,11 @@ private async void OnFolderAnimationClick(object sender, RoutedEventArgs e)
227232
if (AssetsFolderName.SelectedItem is TreeItem folder)
228233
{
229234
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.AnimationFolder(cancellationToken, folder); });
235+
FLogger.Append(ELog.Information, () =>
236+
{
237+
FLogger.Text("Successfully saved animations from ", Constants.WHITE);
238+
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true);
239+
});
230240
}
231241
}
232242

FModel/Services/DiscordService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class DiscordHandler
2222

2323
private readonly Assets _staticAssets = new()
2424
{
25-
LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION}"
25+
LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION} ({Constants.APP_SHORT_COMMIT_ID})"
2626
};
2727

2828
private readonly Button[] _buttons =

FModel/ViewModels/CUE4ParseViewModel.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,7 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
912912
SnooperViewer.Run();
913913
return true;
914914
}
915-
case UAnimSequence when isNone && ModelIsWaitingAnimation:
916-
case UAnimMontage when isNone && ModelIsWaitingAnimation:
917-
case UAnimComposite when isNone && ModelIsWaitingAnimation:
915+
case UAnimSequenceBase when isNone && ModelIsWaitingAnimation:
918916
{
919917
SnooperViewer.Renderer.Animate(pointer.Object);
920918
SnooperViewer.Run();
@@ -924,9 +922,7 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
924922
case USkeletalMesh when HasFlag(bulk, EBulkType.Meshes):
925923
case USkeleton when UserSettings.Default.SaveSkeletonAsMesh && HasFlag(bulk, EBulkType.Meshes):
926924
// case UMaterialInstance when HasFlag(bulk, EBulkType.Materials): // read the fucking json
927-
case UAnimSequence when HasFlag(bulk, EBulkType.Animations):
928-
case UAnimMontage when HasFlag(bulk, EBulkType.Animations):
929-
case UAnimComposite when HasFlag(bulk, EBulkType.Animations):
925+
case UAnimSequenceBase when HasFlag(bulk, EBulkType.Animations):
930926
{
931927
SaveExport(pointer.Object.Value, updateUi);
932928
return true;

0 commit comments

Comments
 (0)