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

Update builder api (take 2) #124

Merged
merged 7 commits into from
Dec 1, 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
14 changes: 2 additions & 12 deletions sandbox/Benchmark/Benchmarks/PostLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using log4net.Repository.Hierarchy;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using NLog.Targets.Wrappers;
using Serilog;
using ZLogger;
using ZLogger.Formatters;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Benchmark.Benchmarks;
Expand Down Expand Up @@ -106,16 +103,9 @@ public void SetUp()

var zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(builder =>
logging.AddZLoggerStream(Stream.Null, options =>
{
//builder.AddLogProcessor(new NullProcessor());

//builder.AddStream(Stream.Null);

builder.AddStream(Stream.Null, options =>
{
options.UsePlainTextFormatter(formatter => formatter.SetPrefixFormatter($"{0} [{1}]", (template, info) => template.Format(info.Timestamp, info.LogLevel)));
});
options.UsePlainTextFormatter(formatter => formatter.SetPrefixFormatter($"{0} [{1}]", (template, info) => template.Format(info.Timestamp, info.LogLevel)));
});
});
disposables.Add(zLoggerFactory);
Expand Down
7 changes: 2 additions & 5 deletions sandbox/Benchmark/Benchmarks/WriteJsonToConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ public void SetUpLogger()

zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(builder =>
logging.AddZLoggerConsole(console =>
{
builder.AddConsole(console =>
{
console.UseJsonFormatter();
});
console.UseJsonFormatter();
});
});

Expand Down
13 changes: 4 additions & 9 deletions sandbox/Benchmark/Benchmarks/WriteJsonToFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,11 @@ public void SetUpLogger()

// ZLogger

zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(builder =>
zLoggerFactory = LoggerFactory.Create(logging => logging
.AddZLoggerFile(GetLogFilePath("zlogger.log"), options =>
{
builder.AddFile(GetLogFilePath("zlogger.log"), options =>
{
options.UseJsonFormatter();
});
});
});
options.UseJsonFormatter();
}));

zLogger = zLoggerFactory.CreateLogger<WritePlainTextToFile>();

Expand Down
10 changes: 4 additions & 6 deletions sandbox/Benchmark/Benchmarks/WritePlainTextToConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ public void SetUpLogger()
{
zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(builder =>
logging.AddZLoggerConsole(console =>
{
builder.AddConsole(console =>
console.UsePlainTextFormatter(formatter =>
{
console.UsePlainTextFormatter(formatter =>
{
formatter.SetPrefixFormatter($"{0} [{1}]", (template, info) => template.Format(info.Timestamp, info.LogLevel));
});
formatter.SetPrefixFormatter($"{0} [{1}]",
(template, info) => template.Format(info.Timestamp, info.LogLevel));
});
});
});
Expand Down
16 changes: 6 additions & 10 deletions sandbox/Benchmark/Benchmarks/WritePlainTextToFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,13 @@ public void SetUpLogger()

zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(zLogger =>
logging.AddZLoggerFile(GetLogFilePath("zlogger.log"), options =>
{
zLogger.AddFile(GetLogFilePath("zlogger.log"),
options =>
{
options.UsePlainTextFormatter(formatter =>
{
formatter.SetPrefixFormatter($"{0} [{1}]",
(template, info) => template.Format(info.Timestamp, info.LogLevel));
});
});
options.UsePlainTextFormatter(formatter =>
{
formatter.SetPrefixFormatter($"{0} [{1}]",
(template, info) => template.Format(info.Timestamp, info.LogLevel));
});
});
});

Expand Down
23 changes: 6 additions & 17 deletions sandbox/Benchmark/InternalBenchmarks/EmptyLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,27 @@ public void SetUpLogger()

zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(zLogger =>
{
zLogger.AddLogProcessor(new EmptyLogProcessor());
});
logging.AddZLoggerLogProcessor(new EmptyLogProcessor());
});

zLogger = zLoggerFactory.CreateLogger<EmptyLogging>();

zLoggerFactory2 = LoggerFactory.Create(logging =>
{
logging.AddZLogger(z =>
{
z.AddLogProcessor(options => new WriteUtf8LogProcessor(options));
});
logging.AddZLoggerLogProcessor(options => new WriteUtf8LogProcessor(options));
});

zLogger2 = zLoggerFactory2.CreateLogger<EmptyLogging>();

zLoggerFactory3 = LoggerFactory.Create(logging =>
{
logging.AddZLogger(z =>
logging.AddZLoggerLogProcessor(options =>
{
z.AddLogProcessor(options =>
options.UsePlainTextFormatter(formatter =>
{
options.UsePlainTextFormatter(formatter =>
{
formatter.SetPrefixFormatter($"{0} [{1}]", (template, info) => template.Format(info.Timestamp, info.LogLevel));
});

return new WriteUtf8LogProcessor(options);
formatter.SetPrefixFormatter($"{0} [{1}]", (template, info) => template.Format(info.Timestamp, info.LogLevel));
});

return new WriteUtf8LogProcessor(options);
});
});

Expand Down
2 changes: 1 addition & 1 deletion sandbox/Benchmark/SimpleBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override Microsoft.Extensions.Logging.ILogger GetLogger()
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(logging =>
{
logging.AddZLogger(zLogger => zLogger.AddFile("ZLogger.log"));
logging.AddZLoggerFile("ZLogger.log");
//options.AddZLoggerConsole();
});
var serviceProvider = serviceCollection.BuildServiceProvider();
Expand Down
1 change: 1 addition & 0 deletions sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 2 additions & 24 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
using ConsoleAppFramework;
using ZLogger;
using ZLogger;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Channels;
using Utf8StringInterpolation;
using System.Collections.Concurrent;
using System.Collections.Generic;
using JetBrains.Profiler.Api;
using System.Threading;
using ZLogger.Formatters;
using ZLogger.Internal;
using ZLogger.Providers;
using System.Numerics;
using System.Text.Json;
using System.IO.Hashing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net.Http;
using System.Buffers;
using System.Reflection.PortableExecutable;
using System.Net.Mail;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;





var zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLogger(zLogger =>
{
zLogger.AddLogProcessor(new EmptyLogProcessor());
});
logging.AddZLoggerLogProcessor(new EmptyLogProcessor());
});

var zLogger = zLoggerFactory.CreateLogger("my");
Expand Down
17 changes: 14 additions & 3 deletions src/ZLogger/Providers/ZLoggerRollingFileLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ public enum RollingInterval
Minute
}

public sealed class ZLoggerRollingFileOptions : ZLoggerOptions
{
public Func<DateTimeOffset, int, string>? FilePathSelector { get; set; }
public RollingInterval RollingInterval { get; set; } = RollingInterval.Day;
public int RollingSizeKB { get; set; } = 512 * 1024;
}

[ProviderAlias("ZLoggerRollingFile")]
public class ZLoggerRollingFileLoggerProvider : ILoggerProvider, ISupportExternalScope, IAsyncDisposable
{
readonly ZLoggerOptions options;
readonly ZLoggerRollingFileOptions options;
readonly AsyncStreamLineMessageWriter streamWriter;
IExternalScopeProvider? scopeProvider;

public ZLoggerRollingFileLoggerProvider(Func<DateTimeOffset, int, string> fileNameSelector, RollingInterval rollInterval, int rollSizeKB, ZLoggerOptions options)
public ZLoggerRollingFileLoggerProvider(ZLoggerRollingFileOptions options)
{
if (options.FilePathSelector is null)
{
throw new ArgumentException(nameof(options.FilePathSelector));
}
this.options = options;
var stream = new RollingFileStream(fileNameSelector, rollInterval, rollSizeKB, options.TimeProvider);
var stream = new RollingFileStream(options.FilePathSelector!, options.RollingInterval, options.RollingSizeKB, options.TimeProvider);
this.streamWriter = new AsyncStreamLineMessageWriter(stream, this.options);
}

Expand Down
Loading
Loading