Browse Source

Fixed spelling of LogAdapter, added docstrings

pull/846/head
Christopher F 8 years ago
parent
commit
393cbbeb23
3 changed files with 54 additions and 30 deletions
  1. +10
    -1
      src/Discord.Net.MicrosoftLogging/Extensions.cs
  2. +44
    -0
      src/Discord.Net.MicrosoftLogging/LogAdapter.cs
  3. +0
    -29
      src/Discord.Net.MicrosoftLogging/LogAdaptor.cs

+ 10
- 1
src/Discord.Net.MicrosoftLogging/Extensions.cs View File

@@ -5,9 +5,18 @@ namespace Discord.MicrosoftLogging
{
public static class Extensions
{
/// <summary>
/// Configure this Discord client with support for Microsoft's logging abstractions
/// </summary>
/// <param name="client">The Discord client to hook into</param>
/// <param name="logger">A logger created for logging to</param>
/// <param name="formatter">
/// A custom message formatter, should the default one not suffice.
///
/// See <see cref="LogAdapter(ILogger, Func{LogMessage, Exception, string})"/> for more information.</param>
public static void UseMicrosoftLogging(this IDiscordClient client, ILogger logger, Func<LogMessage, Exception, string> formatter = null)
{
var adaptor = new LogAdaptor(logger, formatter);
var adaptor = new LogAdapter(logger, formatter);
client.Log += adaptor.Log;
}
}


+ 44
- 0
src/Discord.Net.MicrosoftLogging/LogAdapter.cs View File

@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Discord.MicrosoftLogging
{
public class LogAdapter
{
private readonly ILogger _logger;
private readonly Func<LogMessage, Exception, string> _formatter;

/// <summary>
/// Creates a LogAdapter to be used with a Discord client
/// </summary>
/// <param name="logger">The logger implementation that messages will be written to</param>
/// <param name="formatter">
/// A custom message formatter, should the default be inadequate.
///
/// The default message formatter simply returns <see cref="LogMessage.ToString()"/>, which
/// in most cases should not be a problem.
/// </param>
public LogAdapter(ILogger logger, Func<LogMessage, Exception, string> formatter = null)
{
_logger = logger;
_formatter = formatter ?? DefaultFormatter;
}

/// <summary>
/// Convert a Discord.Net log event to an abstract log event
/// </summary>
/// <param name="message">The log event to be converted</param>
/// <returns>A task for compatibility with Discord.Net's async events</returns>
public Task Log(LogMessage message)
{
_logger.Log(GetLogLevel(message.Severity), default(EventId), message, message.Exception, _formatter);
return Task.Delay(0);
}

private string DefaultFormatter(LogMessage message, Exception _)
=> message.ToString();
private static LogLevel GetLogLevel(LogSeverity severity)
=> (LogLevel)(Math.Abs((int)severity - 5));
}
}

+ 0
- 29
src/Discord.Net.MicrosoftLogging/LogAdaptor.cs View File

@@ -1,29 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Discord.MicrosoftLogging
{
public class LogAdaptor
{
private readonly ILogger _logger;
private readonly Func<LogMessage, Exception, string> _formatter;

public LogAdaptor(ILogger logger, Func<LogMessage, Exception, string> formatter = null)
{
_logger = logger;
_formatter = formatter ?? DefaultFormatter;
}

public Task Log(LogMessage message)
{
_logger.Log(GetLogLevel(message.Severity), default(EventId), message, message.Exception, _formatter);
return Task.Delay(0);
}

private string DefaultFormatter(LogMessage message, Exception _)
=> message.ToString();
private static LogLevel GetLogLevel(LogSeverity severity)
=> (LogLevel)(Math.Abs((int)severity - 5));
}
}

Loading…
Cancel
Save