Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FiveM client console Sink #7

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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);
}
}