From 2eab04907a61d7ea2dfac02028eb1389b5a8c796 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sat, 16 Dec 2017 05:01:42 -0500 Subject: [PATCH] C# 7-ify loggers --- .../ElmahInterceptingLogger.cs | 13 +- .../ElmahLogFactory.cs | 127 +++++++++--------- .../EventLogger.cs | 12 +- .../Log4NetLogger.cs | 94 ++++++------- src/ServiceStack.Logging.NLog/NLogLogger.cs | 12 +- .../SerilogLogger.cs | 53 ++++---- src/ServiceStack.Logging.Slack/SlackLog.cs | 5 +- .../SlackLogFactory.cs | 10 +- 8 files changed, 152 insertions(+), 174 deletions(-) diff --git a/src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs b/src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs index cca65ba6265..925d5899e88 100644 --- a/src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs +++ b/src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs @@ -1,5 +1,4 @@ using Elmah; -using ServiceStack.Logging; using System; using System.Web; @@ -23,11 +22,8 @@ public class ElmahInterceptingLogger /// The application to signal with the errors 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) @@ -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) { diff --git a/src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs b/src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs index 54869160354..980534d3a9a 100644 --- a/src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs +++ b/src/ServiceStack.Logging.Elmah/ElmahLogFactory.cs @@ -5,81 +5,80 @@ namespace ServiceStack.Logging.Elmah { - /// - /// 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. - /// - /// 9/2/2011. - public class ElmahLogFactory : ILogFactory - { - - internal class ErrorFilterConsole : ErrorFilterModule + /// + /// 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. + /// + /// 9/2/2011. + 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; - /// Constructor. - /// 9/2/2011. - /// The log factory that provides the original. - /// The Http Application to log with. Optional parameter in case of self hosting. - 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; - } + /// Constructor. + /// 9/2/2011. + /// The log factory that provides the original. + /// The Http Application to log with. Optional parameter in case of self hosting. + 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; - /// Gets a logger from the wrapped logFactory. - /// 9/2/2011. - /// Name of the type. - /// The logger. - public ILog GetLogger(string typeName) - { - return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application); - } + } - /// Gets a logger from the wrapped logFactory. - /// 9/2/2011. - /// The type. - /// The logger. - public ILog GetLogger(Type type) - { - return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application); + /// Gets a logger from the wrapped logFactory. + /// 9/2/2011. + /// Name of the type. + /// The logger. + public ILog GetLogger(string typeName) + { + return new ElmahInterceptingLogger(this.logFactory.GetLogger(typeName), application); + } + + /// Gets a logger from the wrapped logFactory. + /// 9/2/2011. + /// The type. + /// The logger. + public ILog GetLogger(Type type) + { + return new ElmahInterceptingLogger(this.logFactory.GetLogger(type), application); + } } - } } diff --git a/src/ServiceStack.Logging.EventLog/EventLogger.cs b/src/ServiceStack.Logging.EventLog/EventLogger.cs index 773f8498e34..b579f1d2857 100644 --- a/src/ServiceStack.Logging.EventLog/EventLogger.cs +++ b/src/ServiceStack.Logging.EventLog/EventLogger.cs @@ -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; } @@ -54,7 +52,7 @@ private void Write(object message, EventLogEntryType eventLogType) /// Type of the event log. 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)) diff --git a/src/ServiceStack.Logging.Log4Net/Log4NetLogger.cs b/src/ServiceStack.Logging.Log4Net/Log4NetLogger.cs index 9e655f2e1c2..fef4d92cd7e 100644 --- a/src/ServiceStack.Logging.Log4Net/Log4NetLogger.cs +++ b/src/ServiceStack.Logging.Log4Net/Log4NetLogger.cs @@ -7,11 +7,11 @@ namespace ServiceStack.Logging.Log4Net /// public class Log4NetLogger : ILogWithContext { - private readonly log4net.ILog _log; + private readonly log4net.ILog log; public Log4NetLogger(string typeName) { - _log = log4net.LogManager.GetLogger(typeName); + log = log4net.LogManager.GetLogger(typeName); } /// @@ -20,10 +20,10 @@ public Log4NetLogger(string typeName) /// The type. public Log4NetLogger(Type type) { - _log = log4net.LogManager.GetLogger(type); + log = log4net.LogManager.GetLogger(type); } - public bool IsDebugEnabled { get { return _log.IsDebugEnabled; } } + public bool IsDebugEnabled => log.IsDebugEnabled; /// /// Logs a Debug message. @@ -31,8 +31,8 @@ public Log4NetLogger(Type type) /// The message. public void Debug(object message) { - if (_log.IsDebugEnabled) - _log.Debug(message); + if (log.IsDebugEnabled) + log.Debug(message); } /// @@ -42,8 +42,8 @@ public void Debug(object message) /// The exception. public void Debug(object message, Exception exception) { - if (_log.IsDebugEnabled) - _log.Debug(message, exception); + if (log.IsDebugEnabled) + log.Debug(message, exception); } /// @@ -54,8 +54,8 @@ public void Debug(object message, Exception exception) /// The args. public void Debug(Exception exception, string format, params object[] args) { - if (_log.IsDebugEnabled) - _log.Debug(string.Format(format, args), exception); + if (log.IsDebugEnabled) + log.Debug(string.Format(format, args), exception); } /// @@ -65,8 +65,8 @@ public void Debug(Exception exception, string format, params object[] args) /// The args. public void DebugFormat(string format, params object[] args) { - if (_log.IsDebugEnabled) - _log.DebugFormat(format, args); + if (log.IsDebugEnabled) + log.DebugFormat(format, args); } /// @@ -75,8 +75,8 @@ public void DebugFormat(string format, params object[] args) /// The message. public void Error(object message) { - if (_log.IsErrorEnabled) - _log.Error(message); + if (log.IsErrorEnabled) + log.Error(message); } /// @@ -86,8 +86,8 @@ public void Error(object message) /// The exception. public void Error(object message, Exception exception) { - if (_log.IsErrorEnabled) - _log.Error(message, exception); + if (log.IsErrorEnabled) + log.Error(message, exception); } /// @@ -98,8 +98,8 @@ public void Error(object message, Exception exception) /// The args. public void Error(Exception exception, string format, params object[] args) { - if (_log.IsErrorEnabled) - _log.Error(string.Format(format, args), exception); + if (log.IsErrorEnabled) + log.Error(string.Format(format, args), exception); } /// @@ -109,8 +109,8 @@ public void Error(Exception exception, string format, params object[] args) /// The args. public void ErrorFormat(string format, params object[] args) { - if (_log.IsErrorEnabled) - _log.ErrorFormat(format, args); + if (log.IsErrorEnabled) + log.ErrorFormat(format, args); } /// @@ -119,8 +119,8 @@ public void ErrorFormat(string format, params object[] args) /// The message. public void Fatal(object message) { - if (_log.IsFatalEnabled) - _log.Fatal(message); + if (log.IsFatalEnabled) + log.Fatal(message); } /// @@ -130,8 +130,8 @@ public void Fatal(object message) /// The exception. public void Fatal(object message, Exception exception) { - if (_log.IsFatalEnabled) - _log.Fatal(message, exception); + if (log.IsFatalEnabled) + log.Fatal(message, exception); } /// @@ -142,8 +142,8 @@ public void Fatal(object message, Exception exception) /// The args. public void Fatal(Exception exception, string format, params object[] args) { - if (_log.IsFatalEnabled) - _log.Fatal(string.Format(format, args), exception); + if (log.IsFatalEnabled) + log.Fatal(string.Format(format, args), exception); } /// @@ -153,8 +153,8 @@ public void Fatal(Exception exception, string format, params object[] args) /// The args. public void FatalFormat(string format, params object[] args) { - if (_log.IsFatalEnabled) - _log.FatalFormat(format, args); + if (log.IsFatalEnabled) + log.FatalFormat(format, args); } /// @@ -163,8 +163,8 @@ public void FatalFormat(string format, params object[] args) /// The message. public void Info(object message) { - if (_log.IsInfoEnabled) - _log.Info(message); + if (log.IsInfoEnabled) + log.Info(message); } /// @@ -174,8 +174,8 @@ public void Info(object message) /// The exception. public void Info(object message, Exception exception) { - if (_log.IsInfoEnabled) - _log.Info(message, exception); + if (log.IsInfoEnabled) + log.Info(message, exception); } /// @@ -186,8 +186,8 @@ public void Info(object message, Exception exception) /// The args. public void Info(Exception exception, string format, params object[] args) { - if (_log.IsInfoEnabled) - _log.Info(string.Format(format, args), exception); + if (log.IsInfoEnabled) + log.Info(string.Format(format, args), exception); } /// @@ -197,8 +197,8 @@ public void Info(Exception exception, string format, params object[] args) /// The args. public void InfoFormat(string format, params object[] args) { - if (_log.IsInfoEnabled) - _log.InfoFormat(format, args); + if (log.IsInfoEnabled) + log.InfoFormat(format, args); } public IDisposable PushProperty(string key, object value) @@ -213,8 +213,8 @@ public IDisposable PushProperty(string key, object value) /// The message. public void Warn(object message) { - if (_log.IsWarnEnabled) - _log.Warn(message); + if (log.IsWarnEnabled) + log.Warn(message); } /// @@ -224,8 +224,8 @@ public void Warn(object message) /// The exception. public void Warn(object message, Exception exception) { - if (_log.IsWarnEnabled) - _log.Warn(message, exception); + if (log.IsWarnEnabled) + log.Warn(message, exception); } /// @@ -236,8 +236,8 @@ public void Warn(object message, Exception exception) /// The args. public void Warn(Exception exception, string format, params object[] args) { - if (_log.IsWarnEnabled) - _log.Warn(string.Format(format, args), exception); + if (log.IsWarnEnabled) + log.Warn(string.Format(format, args), exception); } /// @@ -247,22 +247,22 @@ public void Warn(Exception exception, string format, params object[] args) /// The args. public void WarnFormat(string format, params object[] args) { - if (_log.IsWarnEnabled) - _log.WarnFormat(format, args); + if (log.IsWarnEnabled) + log.WarnFormat(format, args); } private class RemovePropertyOnDispose : IDisposable { - private readonly string _removeKey; + private readonly string removeKey; public RemovePropertyOnDispose(string removeKey) { - _removeKey = removeKey; + this.removeKey = removeKey; } public void Dispose() { - log4net.LogicalThreadContext.Properties.Remove(_removeKey); + log4net.LogicalThreadContext.Properties.Remove(removeKey); } } } diff --git a/src/ServiceStack.Logging.NLog/NLogLogger.cs b/src/ServiceStack.Logging.NLog/NLogLogger.cs index 343ba33a0c7..1188b029b3c 100644 --- a/src/ServiceStack.Logging.NLog/NLogLogger.cs +++ b/src/ServiceStack.Logging.NLog/NLogLogger.cs @@ -26,19 +26,19 @@ public NLogLogger(Type type) public static bool UseFullTypeNames { get; set; } - public bool IsDebugEnabled { get { return log.IsDebugEnabled; } } + public bool IsDebugEnabled => log.IsDebugEnabled; - public bool IsInfoEnabled { get { return log.IsInfoEnabled; } } + public bool IsInfoEnabled => log.IsInfoEnabled; - public bool IsWarnEnabled { get { return log.IsWarnEnabled; } } + public bool IsWarnEnabled => log.IsWarnEnabled; - public bool IsErrorEnabled { get { return log.IsErrorEnabled; } } + public bool IsErrorEnabled => log.IsErrorEnabled; - public bool IsFatalEnabled { get { return log.IsFatalEnabled; } } + public bool IsFatalEnabled => log.IsFatalEnabled; private static string AsString(object message) { - return message != null ? message.ToString() : null; + return message?.ToString(); } /// diff --git a/src/ServiceStack.Logging.Serilog/SerilogLogger.cs b/src/ServiceStack.Logging.Serilog/SerilogLogger.cs index f0fd1be9927..5b238135ae8 100644 --- a/src/ServiceStack.Logging.Serilog/SerilogLogger.cs +++ b/src/ServiceStack.Logging.Serilog/SerilogLogger.cs @@ -12,8 +12,8 @@ namespace ServiceStack.Logging.Serilog /// public class SerilogLogger : ILogWithContext { - private readonly ILogger _log; - private static Lazy> _pushProperty = new Lazy>(GetPushProperty); + private readonly ILogger log; + private static readonly Lazy> pushProperty = new Lazy>(GetPushProperty); /// /// Initializes a new instance of the class. @@ -21,38 +21,38 @@ public class SerilogLogger : ILogWithContext /// The . public SerilogLogger(Type type) { - _log = Log.ForContext(type); + log = Log.ForContext(type); } public SerilogLogger(ILogger log) { - _log = log; + this.log = log; } /// /// Gets a value indicating if Debug messages are enabled. /// - public bool IsDebugEnabled => _log.IsEnabled(LogEventLevel.Debug); + public bool IsDebugEnabled => log.IsEnabled(LogEventLevel.Debug); /// /// Gets a value indicating if Info messages are enabled. /// - public bool IsInfoEnabled => _log.IsEnabled(LogEventLevel.Information); + public bool IsInfoEnabled => log.IsEnabled(LogEventLevel.Information); /// /// Gets a value indicating if Warning messages are enabled. /// - public bool IsWarnEnabled => _log.IsEnabled(LogEventLevel.Warning); + public bool IsWarnEnabled => log.IsEnabled(LogEventLevel.Warning); /// /// Gets a value indicating if Error messages are enabled. /// - public bool IsErrorEnabled => _log.IsEnabled(LogEventLevel.Error); + public bool IsErrorEnabled => log.IsEnabled(LogEventLevel.Error); /// /// Gets a value indicating if Fatal messages are enabled. /// - public bool IsFatalEnabled => _log.IsEnabled(LogEventLevel.Fatal); + public bool IsFatalEnabled => log.IsEnabled(LogEventLevel.Fatal); /// /// Logs a Debug message. @@ -241,7 +241,7 @@ public void InfoFormat(string format, params object[] args) /// public IDisposable PushProperty(string key, object value) { - return _pushProperty.Value(key, value, false); + return pushProperty.Value(key, value, false); } private static Func GetPushProperty() @@ -313,68 +313,65 @@ public void WarnFormat(string format, params object[] args) internal ILog ForContext(string propertyName, object value, bool destructureObjects = false) { - return new SerilogLogger(_log.ForContext(propertyName, value, destructureObjects)); + return new SerilogLogger(log.ForContext(propertyName, value, destructureObjects)); } internal ILog ForContext(ILogEventEnricher enricher) { - return new SerilogLogger(_log.ForContext(enricher)); + return new SerilogLogger(log.ForContext(enricher)); } internal ILog ForContext(IEnumerable enrichers) { - return new SerilogLogger(_log.ForContext(enrichers)); + return new SerilogLogger(log.ForContext(enrichers)); } internal ILog ForContext(Type type) { - return new SerilogLogger(_log.ForContext(type)); + return new SerilogLogger(log.ForContext(type)); } internal ILog ForContext() { - return new SerilogLogger(_log.ForContext()); + return new SerilogLogger(log.ForContext()); } private void Write(LogEventLevel level, object message) { - var messageTemplate = message as string; - if (messageTemplate != null) + if (message is string messageTemplate) { - _log.Write(level, messageTemplate); + log.Write(level, messageTemplate); return; } - var exception = message as Exception; - if (exception != null) + if (message is Exception exception) { - _log.Write(level, exception, exception.GetType().Name); + log.Write(level, exception, exception.GetType().Name); return; } - _log.Write(level, message.ToString()); + log.Write(level, message.ToString()); } private void Write(LogEventLevel level, object message, Exception exception) { - var messageTemplate = message as string; - if (messageTemplate != null) + if (message is string messageTemplate) { - _log.Write(level, exception, messageTemplate); + log.Write(level, exception, messageTemplate); return; } - _log.Write(level, exception, message.ToString()); + log.Write(level, exception, message.ToString()); } private void Write(LogEventLevel level, string format, params object[] args) { - _log.Write(level, format, args); + log.Write(level, format, args); } private void Write(LogEventLevel level, Exception ex, string messageTemplate, params object[] propertyValues) { - _log.Write(level, ex, messageTemplate, propertyValues); + log.Write(level, ex, messageTemplate, propertyValues); } } } diff --git a/src/ServiceStack.Logging.Slack/SlackLog.cs b/src/ServiceStack.Logging.Slack/SlackLog.cs index 92536e54413..824f8fe20ab 100644 --- a/src/ServiceStack.Logging.Slack/SlackLog.cs +++ b/src/ServiceStack.Logging.Slack/SlackLog.cs @@ -235,10 +235,7 @@ public void Warn(Exception exception, string format, params object[] args) Write(string.Format(format, args), exception, WarnChannel); } - public bool IsDebugEnabled - { - get { return debugEnabled; } - } + public bool IsDebugEnabled => debugEnabled; } class SlackLoggingData diff --git a/src/ServiceStack.Logging.Slack/SlackLogFactory.cs b/src/ServiceStack.Logging.Slack/SlackLogFactory.cs index 3e8ef503132..eedd88b6843 100644 --- a/src/ServiceStack.Logging.Slack/SlackLogFactory.cs +++ b/src/ServiceStack.Logging.Slack/SlackLogFactory.cs @@ -11,13 +11,7 @@ public class SlackLogFactory : ILogFactory /// /// Slack Incoming Webhook URL. /// - public string Url - { - get - { - return incomingWebHookUrl; - } - } + public string Url => incomingWebHookUrl; /// /// Default channel override. @@ -108,7 +102,7 @@ public SlackLogFactory(string incomingWebHookUrl, IAppSettings appSettings) public SlackLogFactory(IAppSettings appSettings) { if (appSettings == null) - throw new ArgumentNullException("appSettings"); + throw new ArgumentNullException(nameof(appSettings)); if(incomingWebHookUrl == null) incomingWebHookUrl = appSettings.GetString(ConfigKeyFmt.Fmt("Url"));