Skip to content

Commit

Permalink
add extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 30, 2020
1 parent 0190db7 commit 51d8de2
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 273 deletions.
8 changes: 4 additions & 4 deletions src/ZLogger/AsyncProcessZLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 35,19 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
public IDisposable BeginScope<TState>(TState state)
{
// currently scope is not supported...
return NUllDisposable.Instance;
return NullDisposable.Instance;
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None;
}

class NUllDisposable : IDisposable
class NullDisposable : IDisposable
{
public static IDisposable Instance = new NUllDisposable();
public static IDisposable Instance = new NullDisposable();

NUllDisposable()
NullDisposable()
{

}
Expand Down
4 changes: 2 additions & 2 deletions src/ZLogger/AsyncStreamLineMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 139,9 @@ async Task WriteLoop()
{
try
{
if (options.ErrorLogger != null)
if (options.InternalErrorLogger != null)
{
options.ErrorLogger(ex);
options.InternalErrorLogger(ex);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO;

namespace ZLogger.Providers
{
Expand Down
30 changes: 30 additions & 0 deletions src/ZLogger/Providers/ZLoggerLogProcessorProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,30 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO;

namespace ZLogger.Providers

{

[ProviderAlias("ZLoggerLogProcessorProvider")]
public class ZLoggerLogProcessorProvider : ILoggerProvider
{
IAsyncLogProcessor processor;

public ZLoggerLogProcessorProvider(IAsyncLogProcessor processor, IOptions<ZLoggerOptions> options)
{
this.processor = processor;
}

public ILogger CreateLogger(string categoryName)
{
return new AsyncProcessZLogger(categoryName, processor);
}

public void Dispose()
{
processor.DisposeAsync().AsTask().Wait();
}
}
}
29 changes: 29 additions & 0 deletions src/ZLogger/Providers/ZLoggerStreamLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,29 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO;

namespace ZLogger.Providers
{

[ProviderAlias("ZLoggerStream")]
public class ZLoggerStreamLoggerProvider : ILoggerProvider
{
AsyncStreamLineMessageWriter streamWriter;

public ZLoggerStreamLoggerProvider(Stream stream, IOptions<ZLoggerOptions> options)
{
this.streamWriter = new AsyncStreamLineMessageWriter(stream, options.Value);
}

public ILogger CreateLogger(string categoryName)
{
return new AsyncProcessZLogger(categoryName, streamWriter);
}

public void Dispose()
{
streamWriter.DisposeAsync().AsTask().Wait();
}
}
}
508 changes: 254 additions & 254 deletions src/ZLogger/ZLoggerExtensions.cs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/ZLogger/ZLoggerExtensions.tt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ namespace ZLogger
ZLog<<#= CreateTypeArgument(i) #>>(logger, logLevel, eventId, null, format, <#= CreateParameterNames(i) #>);
}

public static void ZLog<<#= CreateTypeArgument(i) #>>(this ILogger logger, LogLevel logLevel, Exception exception, string format, <#= CreateParameters(i) #>)
public static void ZLog<<#= CreateTypeArgument(i) #>>(this ILogger logger, LogLevel logLevel, Exception? exception, string format, <#= CreateParameters(i) #>)
{
ZLog<<#= CreateTypeArgument(i) #>>(logger, logLevel, default, exception, format, <#= CreateParameterNames(i) #>);
}
Expand All @@ -71,7 71,7 @@ namespace ZLogger
ZLog<TPayload, <#= CreateTypeArgument(i) #>>(logger, logLevel, eventId, null, payload, format, <#= CreateParameterNames(i) #>);
}

public static void ZLog<TPayload, <#= CreateTypeArgument(i) #>>(this ILogger logger, LogLevel logLevel, Exception exception, TPayload payload, string format, <#= CreateParameters(i) #>)
public static void ZLog<TPayload, <#= CreateTypeArgument(i) #>>(this ILogger logger, LogLevel logLevel, Exception? exception, TPayload payload, string format, <#= CreateParameters(i) #>)
{
ZLog<TPayload, <#= CreateTypeArgument(i) #>>(logger, logLevel, default, exception, payload, format, <#= CreateParameterNames(i) #>);
}
Expand All @@ -95,7 95,7 @@ namespace ZLogger
ZLog<<#= CreateTypeArgument(i) #>>(logger, LogLevel.<#= logLevel #>, eventId, null, format, <#= CreateParameterNames(i) #>);
}

public static void ZLog<#= logLevel #><<#= CreateTypeArgument(i) #>>(this ILogger logger, Exception exception, string format, <#= CreateParameters(i) #>)
public static void ZLog<#= logLevel #><<#= CreateTypeArgument(i) #>>(this ILogger logger, Exception? exception, string format, <#= CreateParameters(i) #>)
{
ZLog<<#= CreateTypeArgument(i) #>>(logger, LogLevel.<#= logLevel #>, default, exception, format, <#= CreateParameterNames(i) #>);
}
Expand All @@ -115,7 115,7 @@ namespace ZLogger
ZLog<TPayload, <#= CreateTypeArgument(i) #>>(logger, LogLevel.<#= logLevel #>, eventId, null, payload, format, <#= CreateParameterNames(i) #>);
}

public static void ZLog<#= logLevel #><TPayload, <#= CreateTypeArgument(i) #>>(this ILogger logger, Exception exception, TPayload payload, string format, <#= CreateParameters(i) #>)
public static void ZLog<#= logLevel #><TPayload, <#= CreateTypeArgument(i) #>>(this ILogger logger, Exception? exception, TPayload payload, string format, <#= CreateParameters(i) #>)
{
ZLog<TPayload, <#= CreateTypeArgument(i) #>>(logger, LogLevel.<#= logLevel #>, default, exception, payload, format, <#= CreateParameterNames(i) #>);
}
Expand All @@ -138,7 138,7 @@ namespace ZLogger
ZLogMessage(logger, logLevel, eventId, null, message);
}

public static void ZLogMessage(this ILogger logger, LogLevel logLevel, Exception exception, string message)
public static void ZLogMessage(this ILogger logger, LogLevel logLevel, Exception? exception, string message)
{
ZLogMessage<object>(logger, logLevel, default, exception, null!, message);
}
Expand All @@ -161,7 161,7 @@ namespace ZLogger
ZLogMessage<TPayload>(logger, logLevel, eventId, null, payload, message);
}

public static void ZLogMessage<TPayload>(this ILogger logger, LogLevel logLevel, Exception exception, TPayload payload, string message)
public static void ZLogMessage<TPayload>(this ILogger logger, LogLevel logLevel, Exception? exception, TPayload payload, string message)
{
ZLogMessage<TPayload>(logger, logLevel, default, exception, payload, message);
}
Expand All @@ -185,7 185,7 @@ namespace ZLogger
ZLog<#= logLevel #>Message(logger, eventId, null, message);
}

public static void ZLog<#= logLevel #>Message(this ILogger logger, Exception exception, string message)
public static void ZLog<#= logLevel #>Message(this ILogger logger, Exception? exception, string message)
{
ZLog<#= logLevel #>Message<object>(logger, default, exception, null!, message);
}
Expand All @@ -208,7 208,7 @@ namespace ZLogger
ZLog<#= logLevel #>Message<TPayload>(logger, eventId, null, payload, message);
}

public static void ZLog<#= logLevel #>Message<TPayload>(this ILogger logger, Exception exception, TPayload payload, string message)
public static void ZLog<#= logLevel #>Message<TPayload>(this ILogger logger, Exception? exception, TPayload payload, string message)
{
ZLog<#= logLevel #>Message<TPayload>(logger, default, exception, payload, message);
}
Expand Down
47 changes: 47 additions & 0 deletions src/ZLogger/ZLoggerLoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Text;
using ZLogger.Providers;

Expand Down Expand Up @@ -39,6 40,52 @@ public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, Ac
return builder;
}

public static ILoggingBuilder AddZLoggerStream(this ILoggingBuilder builder, Stream stream)
{
builder.AddConfiguration();

builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerStreamLoggerProvider>(x => new ZLoggerStreamLoggerProvider(stream, x.GetService<IOptions<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerStreamLoggerProvider>(builder.Services);

return builder;
}

public static ILoggingBuilder AddZLoggerStream(this ILoggingBuilder builder, Stream stream, Action<ZLoggerOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.AddZLoggerStream(stream);
builder.Services.Configure(configure);

return builder;
}

public static ILoggingBuilder AddZLoggerLogProcessor(this ILoggingBuilder builder, IAsyncLogProcessor logProcessor)
{
builder.AddConfiguration();

builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerLogProcessorProvider>(x => new ZLoggerLogProcessorProvider(logProcessor, x.GetService<IOptions<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerLogProcessorProvider>(builder.Services);

return builder;
}

public static ILoggingBuilder AddZLoggerLogProcessor(this ILoggingBuilder builder, IAsyncLogProcessor logProcessor, Action<ZLoggerOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.AddZLoggerLogProcessor(logProcessor);
builder.Services.Configure(configure);

return builder;
}

public static ILoggingBuilder AddZLoggerFile(this ILoggingBuilder builder, string fileName)
{
builder.AddConfiguration();
Expand Down
8 changes: 3 additions & 5 deletions src/ZLogger/ZLoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 1,5 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Encodings.Web;
Expand All @@ -13,7 11,7 @@ namespace ZLogger
{
public class ZLoggerOptions
{
public Action<Exception>? ErrorLogger { get; set; }
public Action<Exception>? InternalErrorLogger { get; set; }
public CancellationToken CancellationToken { get; set; }

// Options for Text logging
Expand Down Expand Up @@ -140,13 138,13 @@ static void Write(IBufferWriter<byte> writer, string message1, string message2,

internal void LogException(Exception ex)
{
if (ErrorLogger == null)
if (InternalErrorLogger == null)
{
Console.WriteLine(ex);
}
else
{
ErrorLogger(ex);
InternalErrorLogger(ex);
}
}
}
Expand Down

0 comments on commit 51d8de2

Please sign in to comment.