diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-04-07 08:42:03 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-04-07 08:42:03 -0700 |
commit | e4c746826690259eddba106e8a44d1b52b542faf (patch) | |
tree | 210915996e4dd5fd65bcf31e79be25f38213554e /src | |
parent | b4266c2cd8992023ad926a20406fbfe9bcc9cf49 (diff) | |
parent | 4f9a11bd3005663a1287313c4b0755dcdc1f2045 (diff) | |
download | DotNetOpenAuth-e4c746826690259eddba106e8a44d1b52b542faf.zip DotNetOpenAuth-e4c746826690259eddba106e8a44d1b52b542faf.tar.gz DotNetOpenAuth-e4c746826690259eddba106e8a44d1b52b542faf.tar.bz2 |
Merge pull request #265 from skurdiukov/nlog_support
Added support for NLog2 logger.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj | 5 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Core/Logger.cs | 2 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Core/Loggers/NLogLogger.cs | 222 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Core/packages.config | 1 |
4 files changed, 229 insertions, 1 deletions
diff --git a/src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj b/src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj index 357b49a..ebfd000 100644 --- a/src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj +++ b/src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj @@ -130,6 +130,7 @@ <Compile Include="Logger.cs" /> <Compile Include="Loggers\ILog.cs" /> <Compile Include="Loggers\Log4NetLogger.cs" /> + <Compile Include="Loggers\NLogLogger.cs" /> <Compile Include="Loggers\NoOpLogger.cs" /> <Compile Include="Loggers\TraceLogger.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> @@ -172,6 +173,10 @@ <SpecificVersion>False</SpecificVersion> <HintPath>..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath> </Reference> + <Reference Include="NLog"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath> + </Reference> <Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath> diff --git a/src/DotNetOpenAuth.Core/Logger.cs b/src/DotNetOpenAuth.Core/Logger.cs index cf9343b..d14fd8a 100644 --- a/src/DotNetOpenAuth.Core/Logger.cs +++ b/src/DotNetOpenAuth.Core/Logger.cs @@ -177,7 +177,7 @@ namespace DotNetOpenAuth { /// <param name="name">The name of the log to initialize.</param> /// <returns>The <see cref="ILog"/> instance of the logger to use.</returns> private static ILog InitializeFacade(string name) { - ILog result = Log4NetLogger.Initialize(name) ?? TraceLogger.Initialize(name) ?? NoOpLogger.Initialize(); + ILog result = Log4NetLogger.Initialize(name) ?? NLogLogger.Initialize(name) ?? TraceLogger.Initialize(name) ?? NoOpLogger.Initialize(); return result; } } diff --git a/src/DotNetOpenAuth.Core/Loggers/NLogLogger.cs b/src/DotNetOpenAuth.Core/Loggers/NLogLogger.cs new file mode 100644 index 0000000..b5d883d --- /dev/null +++ b/src/DotNetOpenAuth.Core/Loggers/NLogLogger.cs @@ -0,0 +1,222 @@ +// <auto-generated /> + +namespace DotNetOpenAuth.Loggers { + using System; + using System.Globalization; + using System.IO; + using System.Reflection; + + internal class NLogLogger : ILog { + private NLog.Logger nLogLogger; + + private NLogLogger(NLog.Logger logger) { + this.nLogLogger = logger; + } + + #region ILog Members + + public bool IsDebugEnabled { + get { return this.nLogLogger.IsDebugEnabled; } + } + + public bool IsInfoEnabled { + get { return this.nLogLogger.IsInfoEnabled; } + } + + public bool IsWarnEnabled { + get { return this.nLogLogger.IsWarnEnabled; } + } + + public bool IsErrorEnabled { + get { return this.nLogLogger.IsErrorEnabled; } + } + + public bool IsFatalEnabled { + get { return this.nLogLogger.IsFatalEnabled; } + } + + #endregion + + private static bool IsNLogPresent { + get { + try { + Assembly.Load("NLog"); + return true; + } catch (FileNotFoundException) { + return false; + } + } + } + + #region ILog methods + + public void Debug(object message) { + this.nLogLogger.Debug(message); + } + + public void Debug(object message, Exception exception) { + this.nLogLogger.DebugException(String.Format("{0}", message), exception); + } + + public void DebugFormat(string format, params object[] args) { + this.nLogLogger.Debug(CultureInfo.InvariantCulture, format, args); + } + + public void DebugFormat(string format, object arg0) { + this.nLogLogger.Debug(format, arg0); + } + + public void DebugFormat(string format, object arg0, object arg1) { + this.nLogLogger.Debug(format, arg0, arg1); + } + + public void DebugFormat(string format, object arg0, object arg1, object arg2) { + this.nLogLogger.Debug(format, arg0, arg1, arg2); + } + + public void DebugFormat(IFormatProvider provider, string format, params object[] args) { + this.nLogLogger.Debug(provider, format, args); + } + + public void Info(object message) { + this.nLogLogger.Info(message); + } + + public void Info(object message, Exception exception) { + this.nLogLogger.InfoException(String.Format("{0}", message), exception); + } + + public void InfoFormat(string format, params object[] args) { + this.nLogLogger.Info(CultureInfo.InvariantCulture, format, args); + } + + public void InfoFormat(string format, object arg0) { + this.nLogLogger.Info(format, arg0); + } + + public void InfoFormat(string format, object arg0, object arg1) { + this.nLogLogger.Info(format, arg0, arg1); + } + + public void InfoFormat(string format, object arg0, object arg1, object arg2) { + this.nLogLogger.Info(format, arg0, arg1, arg2); + } + + public void InfoFormat(IFormatProvider provider, string format, params object[] args) { + this.nLogLogger.Info(provider, format, args); + } + + public void Warn(object message) { + this.nLogLogger.Warn(message); + } + + public void Warn(object message, Exception exception) { + this.nLogLogger.Warn(String.Format("{0}", message), exception); + } + + public void WarnFormat(string format, params object[] args) { + this.nLogLogger.Warn(CultureInfo.InvariantCulture, format, args); + } + + public void WarnFormat(string format, object arg0) { + this.nLogLogger.Warn(format, arg0); + } + + public void WarnFormat(string format, object arg0, object arg1) { + this.nLogLogger.Warn(format, arg0, arg1); + } + + public void WarnFormat(string format, object arg0, object arg1, object arg2) { + this.nLogLogger.Warn(format, arg0, arg1, arg2); + } + + public void WarnFormat(IFormatProvider provider, string format, params object[] args) { + this.nLogLogger.Warn(provider, format, args); + } + + public void Error(object message) { + this.nLogLogger.Error(message); + } + + public void Error(object message, Exception exception) { + this.nLogLogger.Error(String.Format("{0}", message), exception); + } + + public void ErrorFormat(string format, params object[] args) { + this.nLogLogger.Error(CultureInfo.InvariantCulture, format, args); + } + + public void ErrorFormat(string format, object arg0) { + this.nLogLogger.Error(format, arg0); + } + + public void ErrorFormat(string format, object arg0, object arg1) { + this.nLogLogger.Error(format, arg0, arg1); + } + + public void ErrorFormat(string format, object arg0, object arg1, object arg2) { + this.nLogLogger.Error(format, arg0, arg1, arg2); + } + + public void ErrorFormat(IFormatProvider provider, string format, params object[] args) { + this.nLogLogger.Error(provider, format, args); + } + + public void Fatal(object message) { + this.nLogLogger.Fatal(message); + } + + public void Fatal(object message, Exception exception) { + this.nLogLogger.Fatal(String.Format("{0}", message), exception); + } + + public void FatalFormat(string format, params object[] args) { + this.nLogLogger.Fatal(CultureInfo.InvariantCulture, format, args); + } + + public void FatalFormat(string format, object arg0) { + this.nLogLogger.Fatal(format, arg0); + } + + public void FatalFormat(string format, object arg0, object arg1) { + this.nLogLogger.Fatal(format, arg0, arg1); + } + + public void FatalFormat(string format, object arg0, object arg1, object arg2) { + this.nLogLogger.Fatal(format, arg0, arg1, arg2); + } + + public void FatalFormat(IFormatProvider provider, string format, params object[] args) { + this.nLogLogger.Fatal(provider, format, args); + } + + #endregion + + /// <summary> + /// Returns a new NLog logger if it exists, or returns null if the assembly cannot be found. + /// </summary> + /// <returns>The created <see cref="ILog"/> instance.</returns> + internal static ILog Initialize(string name) { + try { + return IsNLogPresent ? CreateLogger(name) : null; + } catch (FileLoadException) { + // wrong NLog.dll version + return null; + } catch (TargetInvocationException) { + // Thrown due to some security issues on .NET 4.5. + return null; + } catch (TypeLoadException) { + // Thrown by mono (http://stackoverflow.com/questions/10805773/error-when-pushing-dotnetopenauth-to-staging-or-production-environment) + return null; + } + } + + /// <summary> + /// Creates the NLogLogger. Call ONLY after NLog.dll is known to be present. + /// </summary> + /// <returns>The created <see cref="ILog"/> instance.</returns> + private static ILog CreateLogger(string name) { + return new NLogLogger(NLog.LogManager.GetLogger(name)); + } + } +} diff --git a/src/DotNetOpenAuth.Core/packages.config b/src/DotNetOpenAuth.Core/packages.config index 6d6cb3b..ba1d0bf 100644 --- a/src/DotNetOpenAuth.Core/packages.config +++ b/src/DotNetOpenAuth.Core/packages.config @@ -6,5 +6,6 @@ <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net45" /> <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> + <package id="NLog" version="2.0.0.2000" targetFramework="net40" /> <package id="Validation" version="2.0.2.13022" targetFramework="net45" /> </packages>
\ No newline at end of file |