Skip to content

Commit

Permalink
Add FiveM client console Sink
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Dec 30, 2023
1 parent 950e30c commit 2c07ba0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Serilog/Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="" />
<PackageReference Include="CitizenFX.Core.Client" Version="1.0.*" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
23 changes: 23 additions & 0 deletions src/Serilog/Sinks/FiveMSink.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
59 changes: 59 additions & 0 deletions src/Serilog/Sinks/FiveMSinkConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Serilog.Formatting;
using Serilog.Formatting.Display;

namespace Serilog.Sinks;

/// <summary>
/// Static extensions for <see cref="LoggerSinkConfiguration"/>
/// </summary>
public static class FiveMSinkConfigurationExtensions
{
const string DefaultConsoleOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}";

/// <summary>
/// Writes log events to the FiveM client console.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <returns>Configuration object allowing method chaining.</returns>
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);
}

/// <summary>
/// Writes log events to the FiveM client console.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">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.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <returns>Configuration object allowing method chaining.</returns>
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);
}
}

0 comments on commit 2c07ba0

Please sign in to comment.