using NLog;
using System.Web;
namespace Sandbox.Diagnostics;
[StackTraceHidden]
public class Logger
{
private NLog.Logger _log;
///
/// Name of this logger.
///
public string Name { get; protected set; }
public Logger( string name )
{
Logging.InitializeConfig();
Name = name;
_log = NLog.LogManager.GetLogger( name );
Logging.RegisterLogger( Name );
}
internal void DoInfo( FormattableString message ) => WriteToTargets( NLog.LogLevel.Info, null, message );
internal void DoTrace( FormattableString message ) => WriteToTargets( NLog.LogLevel.Trace, null, message );
internal void DoWarning( FormattableString message ) => WriteToTargets( NLog.LogLevel.Warn, null, message );
internal void DoError( FormattableString message ) => WriteToTargets( NLog.LogLevel.Error, null, message );
///
public void Info( FormattableString message ) => DoInfo( message );
///
public void Trace( FormattableString message ) => DoTrace( message );
///
public void Warning( FormattableString message ) => DoWarning( message );
///
public void Error( FormattableString message ) => DoError( message );
///
public void Error( Exception exception, FormattableString message ) => WriteToTargets( NLog.LogLevel.Error, exception, message );
///
/// Log an exception as an error, with given message override.
///
/// The exception to log.
/// The text to override exceptions' message with in the log.
public void Error( Exception exception, object message ) => Error( exception, $"{message}" );
///
/// Log an exception as an error.
///
/// The exception to log.
public void Error( Exception exception ) => WriteToTargets( NLog.LogLevel.Error, exception, $"{exception.Message}" );
///
public void Warning( Exception exception, FormattableString message ) => WriteToTargets( NLog.LogLevel.Warn, exception, message );
///
/// Log an exception as a warning, with given message override.
///
/// The exception to log.
/// The text to override exceptions' message with in the log.
public void Warning( Exception exception, object message ) => Warning( exception, $"{message}" );
///
/// Log some information. This is the default log severity level.
///
/// The information to log.
public void Info( object message )
{
if ( message is Exception ex )
{
Warning( ex, ex.Message );
return;
}
Info( $"{message}" );
}
///
/// Log some information. This is least severe log level.
///
/// The information to log.
public void Trace( object message )
{
if ( message is Exception ex )
{
Warning( ex, ex.Message );
return;
}
Trace( $"{message}" );
}
///
/// Log a warning. This is the second most severe log level.
///
/// The warning to log.
public void Warning( object message )
{
if ( message is Exception ex )
{
Warning( ex, ex.Message );
return;
}
Warning( $"{message}" );
}
///
/// Log an error. This is the most severe log level.
///
/// The error to log.
public void Error( object message )
{
if ( message is Exception ex )
{
Error( ex, ex.Message );
return;
}
Error( $"{message}" );
}
internal void WriteToTargets( NLog.LogLevel nlogLevel, Exception ex, FormattableString message, string name = null )
{
name ??= Name;
LogLevel level = nlogLevel.Ordinal switch
{
0 => LogLevel.Trace,
1 => LogLevel.Trace,
2 => LogLevel.Info,
3 => LogLevel.Warn,
4 => LogLevel.Error,
5 => LogLevel.Error,
_ => LogLevel.Info,
};
if ( !Logging.ShouldLog( name, level ) )
return;
var defaultMessage = message.ToString();
var arguments = new List