log to hutao

This commit is contained in:
DismissedLight 2024-10-21 17:01:26 +08:00
parent 62dcd1a5c7
commit 41be466d30
5 changed files with 56 additions and 5 deletions

View File

@ -67,6 +67,7 @@ public partial class App : Application
{
loggerConfiguration.WriteTo.RichTextBox(richTextBox, LogEventLevel.Information, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");
}
loggerConfiguration.WriteTo.Sink(new HutaoNamedPipeLogEventSink(), LogEventLevel.Information);
Log.Logger = loggerConfiguration.CreateLogger();
services.AddLogging(c => c.AddSerilog());

View File

@ -92,6 +92,10 @@ internal sealed partial class BGINamedPipe : IDisposable
}
break;
case (PipePacketType.SessionTermination, _):
serverStream.Disconnect();
return;
}
}
}

View File

@ -1,9 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System;
using System.IO.Pipes;
using System.Text;
using System.Text.Json.Serialization;
namespace BetterGenshinImpact.Hutao;
@ -40,6 +37,18 @@ internal sealed partial class HutaoNamedPipe : IDisposable
get => isSupported.Value;
}
public bool TryRedirectLog(string log)
{
if (!IsSupported)
{
return false;
}
byte[] buffer = Encoding.UTF8.GetBytes(log);
clientStream.Write(buffer, 0, buffer.Length);
return true;
}
public void Dispose()
{
clientStream.Dispose();

View File

@ -0,0 +1,37 @@
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Display;
using System.IO;
namespace BetterGenshinImpact.Hutao;
internal sealed class HutaoNamedPipeLogEventSink : ILogEventSink
{
private readonly MessageTemplateTextFormatter textFormatter = new("[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");
private readonly MemoryStream buffer;
private readonly TextWriter writer;
private readonly TextReader reader;
private HutaoNamedPipe? namedPipe;
public HutaoNamedPipeLogEventSink()
{
buffer = new();
writer = new StreamWriter(buffer);
reader = new StreamReader(buffer);
}
private HutaoNamedPipe NamedPipe
{
get => namedPipe ??= App.GetService<HutaoNamedPipe>()!;
}
public void Emit(LogEvent logEvent)
{
textFormatter.Format(logEvent, writer);
buffer.Position = 0;
NamedPipe.TryRedirectLog(reader.ReadToEnd());
buffer.SetLength(0);
}
}

View File

@ -395,8 +395,8 @@
<Run Text="启动参数" />
<ui:HyperlinkButton Grid.Row="1"
Margin="0,0,0,0"
Command="{Binding OpenGameCommandLineDocumentCommand}"
Padding="0"
Command="{Binding OpenGameCommandLineDocumentCommand}"
Cursor="Hand">
<ui:HyperlinkButton.Content>
<TextBlock FontSize="11" Text="打开文档" />