Instantly share code, notes, and snippets.

@jaredcnance /LICENSE
Last active Oct 16, 2017

Embed
What would you like to do?
namespace Functions.Logging
{
/// <summary>
/// Decorates log4net ILog with the TraceWriter instance
/// </summary>
public class TraceWriterLoggerFacade : ILog
{
private readonly TraceWriter _traceWriter;
private readonly ILog _log;
public ILogger Logger => _log.Logger;
public TraceWriterLoggerFacade(Type caller, TraceWriter traceWriter)
{
_log = LogManager.GetLogger(caller);
_traceWriter = traceWriter;
}
public void Debug(object message)
{
_traceWriter.Verbose(message.ToString());
_log.Debug(message);
}
public void Debug(object message, Exception exception)
{
_traceWriter.Verbose(message.ToString());
_log.Debug(message);
}
public void DebugFormat(string format, params object[] args)
{
_traceWriter.Verbose(string.Format(format));
_log.DebugFormat(format, args);
}
public void DebugFormat(string format, object arg0)
{
_traceWriter.Verbose(string.Format(format));
_log.DebugFormat(format, arg0);
}
public void DebugFormat(string format, object arg0, object arg1)
{
_traceWriter.Verbose(string.Format(format));
_log.DebugFormat(format, arg0, arg1);
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
_traceWriter.Verbose(string.Format(format));
_log.DebugFormat(format, arg0, arg1, arg2);
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
_traceWriter.Verbose(string.Format(format));
_log.DebugFormat(provider, format, args);
}
public void Info(object message)
{
_traceWriter.Info(message.ToString());
_log.Info(message);
}
public void Info(object message, Exception exception)
{
_traceWriter.Info(message.ToString());
_log.Info(message, exception);
}
public void InfoFormat(string format, params object[] args)
{
_traceWriter.Info(string.Format(format));
_log.InfoFormat(format, args);
}
public void InfoFormat(string format, object arg0)
{
_traceWriter.Info(string.Format(format));
_log.InfoFormat(format, arg0);
}
public void InfoFormat(string format, object arg0, object arg1)
{
_traceWriter.Info(string.Format(format));
_log.InfoFormat(format, arg0, arg1);
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
_traceWriter.Info(string.Format(format));
_log.InfoFormat(format, arg0, arg1, arg2);
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
_traceWriter.Info(string.Format(format));
_log.InfoFormat(provider, format, args);
}
public void Warn(object message)
{
_traceWriter.Warning(message.ToString());
_log.Warn(message);
}
public void Warn(object message, Exception exception)
{
_traceWriter.Warning(message.ToString());
_log.Warn(message, exception);
}
public void WarnFormat(string format, params object[] args)
{
_traceWriter.Warning(string.Format(format));
_log.WarnFormat(format, args);
}
public void WarnFormat(string format, object arg0)
{
_traceWriter.Warning(string.Format(format));
_log.WarnFormat(format, arg0);
}
public void WarnFormat(string format, object arg0, object arg1)
{
_traceWriter.Warning(string.Format(format));
_log.WarnFormat(format, arg0, arg1);
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
_traceWriter.Warning(string.Format(format));
_log.WarnFormat(format, arg0, arg1, arg2);
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
_traceWriter.Warning(string.Format(format));
_log.WarnFormat(provider, format, args);
}
public void Error(object message)
{
_traceWriter.Error(message.ToString());
_log.Error(message);
}
public void Error(object message, Exception exception)
{
_traceWriter.Error(message.ToString(), exception);
_log.Error(message, exception);
}
public void ErrorFormat(string format, params object[] args)
{
_traceWriter.Error(string.Format(format));
_log.ErrorFormat(format, args);
}
public void ErrorFormat(string format, object arg0)
{
_traceWriter.Error(string.Format(format));
_log.ErrorFormat(format, arg0);
}
public void ErrorFormat(string format, object arg0, object arg1)
{
_traceWriter.Error(string.Format(format));
_log.ErrorFormat(format, arg0, arg1);
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
_traceWriter.Error(string.Format(format));
_log.ErrorFormat(format, arg0, arg1, arg2);
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
_traceWriter.Error(string.Format(format));
_log.ErrorFormat(provider, format, args);
}
public void Fatal(object message)
{
_traceWriter.Error(message.ToString());
_log.Fatal(message);
}
public void Fatal(object message, Exception exception)
{
_traceWriter.Error(message.ToString());
_log.Fatal(message, exception);
}
public void FatalFormat(string format, params object[] args)
{
_traceWriter.Error(format);
_log.FatalFormat(format, args);
}
public void FatalFormat(string format, object arg0)
{
_traceWriter.Error(format);
_log.FatalFormat(format, arg0);
}
public void FatalFormat(string format, object arg0, object arg1)
{
_traceWriter.Error(format);
_log.FatalFormat(format, arg0, arg1);
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
_traceWriter.Error(format);
_log.FatalFormat(format, arg0, arg1, arg2);
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
_traceWriter.Error(format);
_log.FatalFormat(provider, format, args);
}
public bool IsDebugEnabled => _log.IsDebugEnabled;
public bool IsInfoEnabled => _log.IsInfoEnabled;
public bool IsWarnEnabled => _log.IsWarnEnabled;
public bool IsErrorEnabled => _log.IsErrorEnabled;
public bool IsFatalEnabled => _log.IsFatalEnabled;
}
}
namespace Functions.Logging
{
/// <summary>
/// Constructs the TraceWriterLogger by passing in the type of the calling
/// class and the resolved TraceWriter instance
/// </summary>
public class TraceWriterLoggingModule : Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry,
IComponentRegistration registration
) => registration.Preparing += OnComponentPreparing;
private void OnComponentPreparing(object sender, PreparingEventArgs e)
{
e.Parameters = e.Parameters.Union(
new[]
{
new ResolvedParameter(
(p, i) => p.ParameterType == typeof(ILog),
(p, i) => new TraceWriterLoggerFacade(p.Member.DeclaringType, e.Context.Resolve<TraceWriter>())
),
});
}
}
}
Copyright (c) 2017 Jared Nance
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment