Skip to content

Commit 2eab049

Browse files
committed
C# 7-ify loggers
1 parent 191641d commit 2eab049

File tree

8 files changed

+152
-174
lines changed

8 files changed

+152
-174
lines changed

src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Elmah;
2-
using ServiceStack.Logging;
32
using System;
43
using System.Web;
54

@@ -23,11 +22,8 @@ public class ElmahInterceptingLogger
2322
/// <param name=application"> The application to signal with the errors </param>
2423
public ElmahInterceptingLogger(ILog log, HttpApplication application)
2524
{
26-
if (null == log) { throw new ArgumentNullException("log"); }
27-
if (null == application) { throw new ArgumentNullException("application"); }
28-
29-
this.log = log;
30-
this.application = application;
25+
this.log = log ?? throw new ArgumentNullException(nameof(log));
26+
this.application = application ?? throw new ArgumentNullException(nameof(application));
3127
}
3228

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

105-
public bool IsDebugEnabled
106-
{
107-
get { return log.IsDebugEnabled; }
108-
}
101+
public bool IsDebugEnabled => log.IsDebugEnabled;
109102

110103
public void Warn(object message, Exception exception)
111104
{

src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,80 @@
55

66
namespace ServiceStack.Logging.Elmah
77
{
8-
/// <summary>
9-
/// Elmah log factory that wraps another log factory, providing interception facilities on log calls. For Error or Fatal calls, the
10-
/// details will be logged to Elmah in addition to the originally intended logger. For all other log types, only the original logger is
11-
/// used.
12-
/// </summary>
13-
/// <remarks> 9/2/2011. </remarks>
14-
public class ElmahLogFactory : ILogFactory
15-
{
16-
17-
internal class ErrorFilterConsole : ErrorFilterModule
8+
/// <summary>
9+
/// Elmah log factory that wraps another log factory, providing interception facilities on log calls. For Error or Fatal calls, the
10+
/// details will be logged to Elmah in addition to the originally intended logger. For all other log types, only the original logger is
11+
/// used.
12+
/// </summary>
13+
/// <remarks> 9/2/2011. </remarks>
14+
public class ElmahLogFactory : ILogFactory
1815
{
19-
public void HookFiltering(IExceptionFiltering module)
20-
{
21-
module.Filtering += new ExceptionFilterEventHandler(base.OnErrorModuleFiltering);
22-
}
23-
}
2416

25-
private readonly ILogFactory logFactory;
26-
private readonly HttpApplication application;
17+
internal class ErrorFilterConsole : ErrorFilterModule
18+
{
19+
public void HookFiltering(IExceptionFiltering module)
20+
{
21+
module.Filtering += base.OnErrorModuleFiltering;
22+
}
23+
}
2724

28-
// Filters and modules for HttpApplication-less Elmah logging
29-
private ErrorFilterConsole errorFilter = new ErrorFilterConsole();
30-
public ErrorMailModule ErrorEmail = new ErrorMailModule();
31-
public ErrorLogModule ErrorLog = new ErrorLogModule();
32-
public ErrorTweetModule ErrorTweet = new ErrorTweetModule();
25+
private readonly ILogFactory logFactory;
26+
private readonly HttpApplication application;
3327

34-
/// <summary> Constructor. </summary>
35-
/// <remarks> 9/2/2011. </remarks>
36-
/// <param name="logFactory"> The log factory that provides the original. </param>
37-
/// <param name="application"> The Http Application to log with. Optional parameter in case of self hosting.</param>
38-
public ElmahLogFactory(ILogFactory logFactory, HttpApplication application = null)
39-
{
40-
if (null == logFactory) { throw new ArgumentNullException("logFactory"); }
41-
if (null == application)
42-
{
43-
application = InitNoContext();
44-
}
28+
// Filters and modules for HttpApplication-less Elmah logging
29+
private ErrorFilterConsole errorFilter = new ErrorFilterConsole();
30+
public ErrorMailModule ErrorEmail = new ErrorMailModule();
31+
public ErrorLogModule ErrorLog = new ErrorLogModule();
32+
public ErrorTweetModule ErrorTweet = new ErrorTweetModule();
4533

46-
this.logFactory = logFactory;
47-
this.application = application;
48-
}
34+
/// <summary> Constructor. </summary>
35+
/// <remarks> 9/2/2011. </remarks>
36+
/// <param name="logFactory"> The log factory that provides the original. </param>
37+
/// <param name="application"> The Http Application to log with. Optional parameter in case of self hosting.</param>
38+
public ElmahLogFactory(ILogFactory logFactory, HttpApplication application = null)
39+
{
40+
if (application == null)
41+
{
42+
application = InitNoContext();
43+
}
4944

50-
private HttpApplication InitNoContext()
51-
{
52-
var httpApplication = new HttpApplication();
53-
errorFilter.Init(httpApplication);
45+
this.logFactory = logFactory ?? throw new ArgumentNullException(nameof(logFactory));
46+
this.application = application;
47+
}
5448

55-
(ErrorEmail as IHttpModule).Init(httpApplication);
56-
errorFilter.HookFiltering(ErrorEmail);
49+
private HttpApplication InitNoContext()
50+
{
51+
var httpApplication = new HttpApplication();
52+
errorFilter.Init(httpApplication);
5753

58-
(ErrorLog as IHttpModule).Init(httpApplication);
59-
errorFilter.HookFiltering(ErrorLog);
54+
(ErrorEmail as IHttpModule).Init(httpApplication);
55+
errorFilter.HookFiltering(ErrorEmail);
6056

61-
(ErrorTweet as IHttpModule).Init(httpApplication);
62-
errorFilter.HookFiltering(ErrorTweet);
63-
return httpApplication;
57+
(ErrorLog as IHttpModule).Init(httpApplication);
58+
errorFilter.HookFiltering(ErrorLog);
6459

65-
}
60+
(ErrorTweet as IHttpModule).Init(httpApplication);
61+
errorFilter.HookFiltering(ErrorTweet);
62+
return httpApplication;
6663

67-
/// <summary> Gets a logger from the wrapped logFactory. </summary>
68-
/// <remarks> 9/2/2011. </remarks>
69-
/// <param name="typeName"> Name of the type. </param>
70-
/// <returns> The logger. </returns>
71-
public ILog GetLogger(string typeName)
72-
{
73-
return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application);
74-
}
64+
}
7565

76-
/// <summary> Gets a logger from the wrapped logFactory. </summary>
77-
/// <remarks> 9/2/2011. </remarks>
78-
/// <param name="type"> The type. </param>
79-
/// <returns> The logger. </returns>
80-
public ILog GetLogger(Type type)
81-
{
82-
return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application);
66+
/// <summary> Gets a logger from the wrapped logFactory. </summary>
67+
/// <remarks> 9/2/2011. </remarks>
68+
/// <param name="typeName"> Name of the type. </param>
69+
/// <returns> The logger. </returns>
70+
public ILog GetLogger(string typeName)
71+
{
72+
return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application);
73+
}
74+
75+
/// <summary> Gets a logger from the wrapped logFactory. </summary>
76+
/// <remarks> 9/2/2011. </remarks>
77+
/// <param name="type"> The type. </param>
78+
/// <returns> The logger. </returns>
79+
public ILog GetLogger(Type type)
80+
{
81+
return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application);
82+
}
8383
}
84-
}
8584
}

src/ServiceStack.Logging.EventLog/EventLogger.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ public class EventLogger : ILogWithException
2323
public EventLogger(string eventLogName, string eventLogSource)
2424
{
2525
if (string.IsNullOrEmpty(eventLogName))
26-
{
27-
throw new ArgumentNullException("eventLogName");
28-
}
26+
throw new ArgumentNullException(nameof(eventLogName));
27+
2928
if (string.IsNullOrEmpty(eventLogSource))
30-
{
31-
throw new ArgumentNullException("eventLogSource");
32-
}
29+
throw new ArgumentNullException(nameof(eventLogSource));
30+
3331
this.eventLogName = eventLogName;
3432
this.eventLogSource = eventLogSource;
3533
}
@@ -54,7 +52,7 @@ private void Write(object message, EventLogEntryType eventLogType)
5452
/// <param name="eventLogType">Type of the event log.</param>
5553
private void Write(object message, Exception exeception, EventLogEntryType eventLogType)
5654
{
57-
StringBuilder sb = new StringBuilder();
55+
var sb = new StringBuilder();
5856

5957
System.Diagnostics.EventLog eventLogger = new System.Diagnostics.EventLog();
6058
if (!System.Diagnostics.EventLog.SourceExists(eventLogSource))

0 commit comments

Comments
 (0)