diff --git a/README.md b/README.md index 180d15f48..f356ed352 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ logger.Information("Hello from the FiveM client!"); - [#3](https://github.com/Twinki14/CitizenFX.Extensions.Client.Serilog/pull/3) - Remove extra performance tests & results, this just bloated things for the purpose of this fork - [#5](https://github.com/Twinki14/CitizenFX.Extensions.Client.Serilog/pull/5) - Un-yield `yield return` and `yield break` uses - [#6](https://github.com/Twinki14/CitizenFX.Extensions.Client.Serilog/pull/6) - Add `get` `set` to PropertyToken._position +- [#7](https://github.com/Twinki14/CitizenFX.Extensions.Client.Serilog/pull/7) - Add FiveM Client Console Sink ## Notes - This fork is in **NO WAY** affiliated with the [Serilog Organization](https://github.com/serilog) or the [Serilog project](https://serilog.net/), it's purely a fork to provide compatability with FiveM's client-resource shipped mono 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); + } +}