Compare commits

..

No commits in common. "7838ec967e31969ad7590eb1a8e00ee7f74b0e67" and "b80fa8cd37884ce0e388727065746e645cffc032" have entirely different histories.

8 changed files with 60 additions and 59 deletions

View file

@ -47,7 +47,7 @@ namespace ExtremelySimpleLogger {
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>

View file

@ -85,7 +85,7 @@ namespace ExtremelySimpleLogger {
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
@ -14,7 +14,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.2.5</VersionPrefix>
</PropertyGroup>
<ItemGroup>

View file

@ -65,7 +65,7 @@ namespace ExtremelySimpleLogger {
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ExtremelySimpleLogger {
/// <summary>
@ -19,16 +18,11 @@ namespace ExtremelySimpleLogger {
/// </summary>
public LogLevel MinimumLevel { get; set; } = LogLevel.Trace;
/// <summary>
/// The <see cref="LogFormatter"/> with which log messages should be formatted by a <see cref="Sink"/> if its <see cref="Sink.Formatter"/> is <see langword="null"/>.
/// By default, <see cref="FormatDefault"/> is used.
/// </summary>
public LogFormatter DefaultFormatter { get; set; } = Logger.FormatDefault;
/// <summary>
/// If this property is set to <code>false</code>, this logger will not log any messages.
/// </summary>
public bool IsEnabled { get; set; } = true;
/// <summary>
/// The name of this logger. This name is used in <see cref="FormatDefault"/> by default.
/// The name of this logger. This name is used in <see cref="Sink.FormatDefault"/> by default.
/// </summary>
public string Name { get; set; }
@ -103,44 +97,5 @@ namespace ExtremelySimpleLogger {
sink.Dispose();
}
/// <summary>
/// The default formatter for logging messages, which is <see cref="DefaultFormatter"/>'s initial value.
/// By default, messages are laid out as follows:
/// <code>
/// [Date and time] [Logger name, if set] [Log level] Message
/// Exception, if set
/// </code>
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="message">The message</param>
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
/// <returns>A formatted string to log</returns>
public static string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) {
var builder = new StringBuilder();
// date
builder.Append($"[{DateTime.Now}] ");
// logger name
if (!string.IsNullOrEmpty(logger.Name))
builder.Append($"[{logger.Name}] ");
// log level
builder.Append($"[{level}] ");
// message
builder.Append(message);
// stack trace
if (e != null)
builder.Append($"\n{e}");
return builder.ToString();
}
}
/// <summary>
/// A delegate method used by <see cref="Sink.Formatter"/> and <see cref="Logger.DefaultFormatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="message">The message</param>
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
public delegate string LogFormatter(Logger logger, LogLevel level, object message, Exception e = null);
}

View file

@ -12,8 +12,8 @@ namespace ExtremelySimpleLogger {
/// </summary>
public LogLevel MinimumLevel { get; set; } = LogLevel.Trace;
/// <summary>
/// The <see cref="LogFormatter"/> with which log messages should be formatted.
/// <see cref="Logger.DefaultFormatter"/> is used if this is <see langword="null"/>.
/// The <see cref="LogFormatter"/> with which this message should be formatted.
/// By default, <see cref="FormatDefault"/> is used.
/// </summary>
public LogFormatter Formatter { get; set; }
/// <summary>
@ -21,6 +21,13 @@ namespace ExtremelySimpleLogger {
/// </summary>
public bool IsEnabled { get; set; } = true;
/// <summary>
/// Initializes a new sink with the default settings.
/// </summary>
public Sink() {
this.Formatter = this.FormatDefault;
}
/// <summary>
/// Logs a message in the way specified by the sink's implementation.
/// </summary>
@ -29,22 +36,61 @@ namespace ExtremelySimpleLogger {
/// <param name="message">The message</param>
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
public virtual void Log(Logger logger, LogLevel level, object message, Exception e = null) {
var formatter = this.Formatter ?? logger.DefaultFormatter;
this.Log(logger, level, formatter.Invoke(logger, level, message, e));
this.Log(logger, level, this.Formatter.Invoke(logger, level, message, e));
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="s">The message to log</param>
protected abstract void Log(Logger logger, LogLevel level, string s);
/// <summary>
/// The default formatter for logging messages.
/// By default, messages are laid out as follows:
/// <code>
/// [Date and time] [Logger name, if set] [Log level] Message
/// Exception, if set
/// </code>
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="message">The message</param>
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
/// <returns>A formatted string to log</returns>
protected virtual string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) {
var builder = new StringBuilder();
// date
builder.Append($"[{DateTime.Now}] ");
// logger name
if (!string.IsNullOrEmpty(logger.Name))
builder.Append($"[{logger.Name}] ");
// log level
builder.Append($"[{level}] ");
// message
builder.Append(message);
// stack trace
if (e != null)
builder.Append($"\n{e}");
return builder.ToString();
}
/// <summary>
/// A delegate method used by <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="message">The message</param>
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
public delegate string LogFormatter(Logger logger, LogLevel level, object message, Exception e = null);
/// <summary>
/// Disposes this sink, freeing all of the resources it uses.
/// </summary>
public virtual void Dispose() {}
public virtual void Dispose() {
}
}
}

View file

@ -31,7 +31,7 @@ namespace ExtremelySimpleLogger {
private readonly StringBuilder builder = new StringBuilder();
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>