From 2c07ba08538c11c0524e9a358ded97eaa84fb051 Mon Sep 17 00:00:00 2001 From: Twinki Date: Sat, 30 Dec 2023 00:35:25 -0500 Subject: [PATCH] Add FiveM client console Sink --- src/Serilog/Serilog.csproj | 1 + src/Serilog/Sinks/FiveMSink.cs | 23 ++++++++ .../Sinks/FiveMSinkConfigurationExtensions.cs | 59 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/Serilog/Sinks/FiveMSink.cs create mode 100644 src/Serilog/Sinks/FiveMSinkConfigurationExtensions.cs diff --git a/src/Serilog/Serilog.csproj b/src/Serilog/Serilog.csproj index ed8b80625..0119d7674 100644 --- a/src/Serilog/Serilog.csproj +++ b/src/Serilog/Serilog.csproj @@ -29,6 +29,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Serilog/Sinks/FiveMSink.cs b/src/Serilog/Sinks/FiveMSink.cs new file mode 100644 index 000000000..96ea3ec46 --- /dev/null +++ b/src/Serilog/Sinks/FiveMSink.cs @@ -0,0 +1,23 @@ +using CitizenFX.Core; +using Serilog.Formatting; + +namespace Serilog.Sinks; + +class FiveMSink : ILogEventSink +{ + readonly ITextFormatter _textFormatter; + + public FiveMSink(ITextFormatter textFormatter) + { + _textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter)); + } + + public void Emit(LogEvent logEvent) + { + if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); + var renderSpace = new StringWriter(); + _textFormatter.Format(logEvent, renderSpace); + + Debug.Write(renderSpace.ToString()); + } +} diff --git a/src/Serilog/Sinks/FiveMSinkConfigurationExtensions.cs b/src/Serilog/Sinks/FiveMSinkConfigurationExtensions.cs new file mode 100644 index 000000000..c9231be25 --- /dev/null +++ b/src/Serilog/Sinks/FiveMSinkConfigurationExtensions.cs @@ -0,0 +1,59 @@ +using Serilog.Formatting; +using Serilog.Formatting.Display; + +namespace Serilog.Sinks; + +/// +/// Static extensions for +/// +public static class FiveMSinkConfigurationExtensions +{ + const string DefaultConsoleOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}"; + + /// + /// Writes log events to the FiveM client console. + /// + /// Logger sink configuration. + /// The minimum level for + /// events passed through the sink. Ignored when is specified. + /// A switch allowing the pass-through minimum level + /// to be changed at runtime. + /// A message template describing the format used to write to the sink. + /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}". + /// Supplies culture-specific formatting information, or null. + /// Configuration object allowing method chaining. + public static LoggerConfiguration FiveM( + this LoggerSinkConfiguration sinkConfiguration, + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, + string outputTemplate = DefaultConsoleOutputTemplate, + IFormatProvider? formatProvider = null, + LoggingLevelSwitch? levelSwitch = null) + { + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); + var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); + return FiveM(sinkConfiguration, formatter, restrictedToMinimumLevel, levelSwitch); + } + + /// + /// Writes log events to the FiveM client console. + /// + /// Logger sink configuration. + /// Controls the rendering of log events into text, for example to log JSON. To + /// control plain text formatting, use the overload that accepts an output template. + /// The minimum level for + /// events passed through the sink. Ignored when is specified. + /// A switch allowing the pass-through minimum level + /// to be changed at runtime. + /// Configuration object allowing method chaining. + public static LoggerConfiguration FiveM( + this LoggerSinkConfiguration sinkConfiguration, + ITextFormatter formatter, + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, + LoggingLevelSwitch? levelSwitch = null) + { + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (formatter == null) throw new ArgumentNullException(nameof(formatter)); + return sinkConfiguration.Sink(new FiveMSink(formatter), restrictedToMinimumLevel, levelSwitch); + } +}