Compare commits

...

3 commits

Author SHA1 Message Date
Ell
7838ec967e 1.3.0 2022-10-05 18:16:19 +02:00
Ell
a1e789a580 multi-target ESL 2022-10-05 18:15:09 +02:00
Ell
ca5a4830c8 made it easier to change the default log formatter 2022-10-05 18:13:29 +02:00
8 changed files with 59 additions and 60 deletions

View file

@ -47,7 +47,7 @@ namespace ExtremelySimpleLogger {
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <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>

View file

@ -85,7 +85,7 @@ namespace ExtremelySimpleLogger {
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <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>

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
@ -14,7 +14,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.2.5</VersionPrefix>
<VersionPrefix>1.3.0</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"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <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>

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ExtremelySimpleLogger {
/// <summary>
@ -18,11 +19,16 @@ 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="Sink.FormatDefault"/> by default.
/// The name of this logger. This name is used in <see cref="FormatDefault"/> by default.
/// </summary>
public string Name { get; set; }
@ -97,5 +103,44 @@ 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 this message should be formatted.
/// By default, <see cref="FormatDefault"/> is used.
/// The <see cref="LogFormatter"/> with which log messages should be formatted.
/// <see cref="Logger.DefaultFormatter"/> is used if this is <see langword="null"/>.
/// </summary>
public LogFormatter Formatter { get; set; }
/// <summary>
@ -21,13 +21,6 @@ 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>
@ -36,61 +29,22 @@ 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) {
this.Log(logger, level, this.Formatter.Invoke(logger, level, message, e));
var formatter = this.Formatter ?? logger.DefaultFormatter;
this.Log(logger, level, formatter.Invoke(logger, level, message, e));
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Formatter"/>.
/// Logs the given message, which has already been formatted using <see cref="Formatter"/> or <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="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"/>.
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <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>

View file

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