Skip to content

Commit

Permalink
C# 7-ify loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Dec 16, 2017
1 parent 191641d commit 2eab049
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 174 deletions.
13 changes: 3 additions & 10 deletions src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs
@@ -1,5 +1,4 @@
using Elmah;
using ServiceStack.Logging;
using System;
using System.Web;

Expand All @@ -23,11 +22,8 @@ public class ElmahInterceptingLogger
/// <param name=application"> The application to signal with the errors </param>
public ElmahInterceptingLogger(ILog log, HttpApplication application)
{
if (null == log) { throw new ArgumentNullException("log"); }
if (null == application) { throw new ArgumentNullException("application"); }

this.log = log;
this.application = application;
this.log = log ?? throw new ArgumentNullException(nameof(log));
this.application = application ?? throw new ArgumentNullException(nameof(application));
}

public void Debug(object message, Exception exception)
Expand Down Expand Up @@ -102,10 +98,7 @@ public void InfoFormat(string format, params object[] args)
log.InfoFormat(format, args);
}

public bool IsDebugEnabled
{
get { return log.IsDebugEnabled; }
}
public bool IsDebugEnabled => log.IsDebugEnabled;

public void Warn(object message, Exception exception)
{
Expand Down
127 changes: 63 additions & 64 deletions src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs
Expand Up @@ -5,81 +5,80 @@

namespace ServiceStack.Logging.Elmah
{
/// <summary>
/// Elmah log factory that wraps another log factory, providing interception facilities on log calls. For Error or Fatal calls, the
/// details will be logged to Elmah in addition to the originally intended logger. For all other log types, only the original logger is
/// used.
/// </summary>
/// <remarks> 9/2/2011. </remarks>
public class ElmahLogFactory : ILogFactory
{

internal class ErrorFilterConsole : ErrorFilterModule
/// <summary>
/// Elmah log factory that wraps another log factory, providing interception facilities on log calls. For Error or Fatal calls, the
/// details will be logged to Elmah in addition to the originally intended logger. For all other log types, only the original logger is
/// used.
/// </summary>
/// <remarks> 9/2/2011. </remarks>
public class ElmahLogFactory : ILogFactory
{
public void HookFiltering(IExceptionFiltering module)
{
module.Filtering += new ExceptionFilterEventHandler(base.OnErrorModuleFiltering);
}
}

private readonly ILogFactory logFactory;
private readonly HttpApplication application;
internal class ErrorFilterConsole : ErrorFilterModule
{
public void HookFiltering(IExceptionFiltering module)
{
module.Filtering += base.OnErrorModuleFiltering;
}
}

// Filters and modules for HttpApplication-less Elmah logging
private ErrorFilterConsole errorFilter = new ErrorFilterConsole();
public ErrorMailModule ErrorEmail = new ErrorMailModule();
public ErrorLogModule ErrorLog = new ErrorLogModule();
public ErrorTweetModule ErrorTweet = new ErrorTweetModule();
private readonly ILogFactory logFactory;
private readonly HttpApplication application;

/// <summary> Constructor. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="logFactory"> The log factory that provides the original. </param>
/// <param name="application"> The Http Application to log with. Optional parameter in case of self hosting.</param>
public ElmahLogFactory(ILogFactory logFactory, HttpApplication application = null)
{
if (null == logFactory) { throw new ArgumentNullException("logFactory"); }
if (null == application)
{
application = InitNoContext();
}
// Filters and modules for HttpApplication-less Elmah logging
private ErrorFilterConsole errorFilter = new ErrorFilterConsole();
public ErrorMailModule ErrorEmail = new ErrorMailModule();
public ErrorLogModule ErrorLog = new ErrorLogModule();
public ErrorTweetModule ErrorTweet = new ErrorTweetModule();

this.logFactory = logFactory;
this.application = application;
}
/// <summary> Constructor. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="logFactory"> The log factory that provides the original. </param>
/// <param name="application"> The Http Application to log with. Optional parameter in case of self hosting.</param>
public ElmahLogFactory(ILogFactory logFactory, HttpApplication application = null)
{
if (application == null)
{
application = InitNoContext();
}

private HttpApplication InitNoContext()
{
var httpApplication = new HttpApplication();
errorFilter.Init(httpApplication);
this.logFactory = logFactory ?? throw new ArgumentNullException(nameof(logFactory));
this.application = application;
}

(ErrorEmail as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorEmail);
private HttpApplication InitNoContext()
{
var httpApplication = new HttpApplication();
errorFilter.Init(httpApplication);

(ErrorLog as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorLog);
(ErrorEmail as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorEmail);

(ErrorTweet as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorTweet);
return httpApplication;
(ErrorLog as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorLog);

}
(ErrorTweet as IHttpModule).Init(httpApplication);
errorFilter.HookFiltering(ErrorTweet);
return httpApplication;

/// <summary> Gets a logger from the wrapped logFactory. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="typeName"> Name of the type. </param>
/// <returns> The logger. </returns>
public ILog GetLogger(string typeName)
{
return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application);
}
}

/// <summary> Gets a logger from the wrapped logFactory. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="type"> The type. </param>
/// <returns> The logger. </returns>
public ILog GetLogger(Type type)
{
return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application);
/// <summary> Gets a logger from the wrapped logFactory. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="typeName"> Name of the type. </param>
/// <returns> The logger. </returns>
public ILog GetLogger(string typeName)
{
return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application);
}

/// <summary> Gets a logger from the wrapped logFactory. </summary>
/// <remarks> 9/2/2011. </remarks>
/// <param name="type"> The type. </param>
/// <returns> The logger. </returns>
public ILog GetLogger(Type type)
{
return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application);
}
}
}
}
12 changes: 5 additions & 7 deletions src/ServiceStack.Logging.EventLog/EventLogger.cs
Expand Up @@ -23,13 +23,11 @@ public class EventLogger : ILogWithException
public EventLogger(string eventLogName, string eventLogSource)
{
if (string.IsNullOrEmpty(eventLogName))
{
throw new ArgumentNullException("eventLogName");
}
throw new ArgumentNullException(nameof(eventLogName));

if (string.IsNullOrEmpty(eventLogSource))
{
throw new ArgumentNullException("eventLogSource");
}
throw new ArgumentNullException(nameof(eventLogSource));

this.eventLogName = eventLogName;
this.eventLogSource = eventLogSource;
}
Expand All @@ -54,7 +52,7 @@ private void Write(object message, EventLogEntryType eventLogType)
/// <param name="eventLogType">Type of the event log.</param>
private void Write(object message, Exception exeception, EventLogEntryType eventLogType)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();

System.Diagnostics.EventLog eventLogger = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists(eventLogSource))
Expand Down

0 comments on commit 2eab049

Please sign in to comment.