Skip to content

Commit 4f9a11b

Browse files
author
Sergey Kurduykov
committed
Added support for NLog2 logger.
1 parent 3f01e9b commit 4f9a11b

4 files changed

Lines changed: 229 additions & 1 deletion

File tree

src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Logger.cs" />
131131
<Compile Include="Loggers\ILog.cs" />
132132
<Compile Include="Loggers\Log4NetLogger.cs" />
133+
<Compile Include="Loggers\NLogLogger.cs" />
133134
<Compile Include="Loggers\NoOpLogger.cs" />
134135
<Compile Include="Loggers\TraceLogger.cs" />
135136
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -172,6 +173,10 @@
172173
<SpecificVersion>False</SpecificVersion>
173174
<HintPath>..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath>
174175
</Reference>
176+
<Reference Include="NLog">
177+
<SpecificVersion>False</SpecificVersion>
178+
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
179+
</Reference>
175180
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
176181
<Private>True</Private>
177182
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>

src/DotNetOpenAuth.Core/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ internal static ILog Create(Type type) {
177177
/// <param name="name">The name of the log to initialize.</param>
178178
/// <returns>The <see cref="ILog"/> instance of the logger to use.</returns>
179179
private static ILog InitializeFacade(string name) {
180-
ILog result = Log4NetLogger.Initialize(name) ?? TraceLogger.Initialize(name) ?? NoOpLogger.Initialize();
180+
ILog result = Log4NetLogger.Initialize(name) ?? NLogLogger.Initialize(name) ?? TraceLogger.Initialize(name) ?? NoOpLogger.Initialize();
181181
return result;
182182
}
183183
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// <auto-generated />
2+
3+
namespace DotNetOpenAuth.Loggers {
4+
using System;
5+
using System.Globalization;
6+
using System.IO;
7+
using System.Reflection;
8+
9+
internal class NLogLogger : ILog {
10+
private NLog.Logger nLogLogger;
11+
12+
private NLogLogger(NLog.Logger logger) {
13+
this.nLogLogger = logger;
14+
}
15+
16+
#region ILog Members
17+
18+
public bool IsDebugEnabled {
19+
get { return this.nLogLogger.IsDebugEnabled; }
20+
}
21+
22+
public bool IsInfoEnabled {
23+
get { return this.nLogLogger.IsInfoEnabled; }
24+
}
25+
26+
public bool IsWarnEnabled {
27+
get { return this.nLogLogger.IsWarnEnabled; }
28+
}
29+
30+
public bool IsErrorEnabled {
31+
get { return this.nLogLogger.IsErrorEnabled; }
32+
}
33+
34+
public bool IsFatalEnabled {
35+
get { return this.nLogLogger.IsFatalEnabled; }
36+
}
37+
38+
#endregion
39+
40+
private static bool IsNLogPresent {
41+
get {
42+
try {
43+
Assembly.Load("NLog");
44+
return true;
45+
} catch (FileNotFoundException) {
46+
return false;
47+
}
48+
}
49+
}
50+
51+
#region ILog methods
52+
53+
public void Debug(object message) {
54+
this.nLogLogger.Debug(message);
55+
}
56+
57+
public void Debug(object message, Exception exception) {
58+
this.nLogLogger.DebugException(String.Format("{0}", message), exception);
59+
}
60+
61+
public void DebugFormat(string format, params object[] args) {
62+
this.nLogLogger.Debug(CultureInfo.InvariantCulture, format, args);
63+
}
64+
65+
public void DebugFormat(string format, object arg0) {
66+
this.nLogLogger.Debug(format, arg0);
67+
}
68+
69+
public void DebugFormat(string format, object arg0, object arg1) {
70+
this.nLogLogger.Debug(format, arg0, arg1);
71+
}
72+
73+
public void DebugFormat(string format, object arg0, object arg1, object arg2) {
74+
this.nLogLogger.Debug(format, arg0, arg1, arg2);
75+
}
76+
77+
public void DebugFormat(IFormatProvider provider, string format, params object[] args) {
78+
this.nLogLogger.Debug(provider, format, args);
79+
}
80+
81+
public void Info(object message) {
82+
this.nLogLogger.Info(message);
83+
}
84+
85+
public void Info(object message, Exception exception) {
86+
this.nLogLogger.InfoException(String.Format("{0}", message), exception);
87+
}
88+
89+
public void InfoFormat(string format, params object[] args) {
90+
this.nLogLogger.Info(CultureInfo.InvariantCulture, format, args);
91+
}
92+
93+
public void InfoFormat(string format, object arg0) {
94+
this.nLogLogger.Info(format, arg0);
95+
}
96+
97+
public void InfoFormat(string format, object arg0, object arg1) {
98+
this.nLogLogger.Info(format, arg0, arg1);
99+
}
100+
101+
public void InfoFormat(string format, object arg0, object arg1, object arg2) {
102+
this.nLogLogger.Info(format, arg0, arg1, arg2);
103+
}
104+
105+
public void InfoFormat(IFormatProvider provider, string format, params object[] args) {
106+
this.nLogLogger.Info(provider, format, args);
107+
}
108+
109+
public void Warn(object message) {
110+
this.nLogLogger.Warn(message);
111+
}
112+
113+
public void Warn(object message, Exception exception) {
114+
this.nLogLogger.Warn(String.Format("{0}", message), exception);
115+
}
116+
117+
public void WarnFormat(string format, params object[] args) {
118+
this.nLogLogger.Warn(CultureInfo.InvariantCulture, format, args);
119+
}
120+
121+
public void WarnFormat(string format, object arg0) {
122+
this.nLogLogger.Warn(format, arg0);
123+
}
124+
125+
public void WarnFormat(string format, object arg0, object arg1) {
126+
this.nLogLogger.Warn(format, arg0, arg1);
127+
}
128+
129+
public void WarnFormat(string format, object arg0, object arg1, object arg2) {
130+
this.nLogLogger.Warn(format, arg0, arg1, arg2);
131+
}
132+
133+
public void WarnFormat(IFormatProvider provider, string format, params object[] args) {
134+
this.nLogLogger.Warn(provider, format, args);
135+
}
136+
137+
public void Error(object message) {
138+
this.nLogLogger.Error(message);
139+
}
140+
141+
public void Error(object message, Exception exception) {
142+
this.nLogLogger.Error(String.Format("{0}", message), exception);
143+
}
144+
145+
public void ErrorFormat(string format, params object[] args) {
146+
this.nLogLogger.Error(CultureInfo.InvariantCulture, format, args);
147+
}
148+
149+
public void ErrorFormat(string format, object arg0) {
150+
this.nLogLogger.Error(format, arg0);
151+
}
152+
153+
public void ErrorFormat(string format, object arg0, object arg1) {
154+
this.nLogLogger.Error(format, arg0, arg1);
155+
}
156+
157+
public void ErrorFormat(string format, object arg0, object arg1, object arg2) {
158+
this.nLogLogger.Error(format, arg0, arg1, arg2);
159+
}
160+
161+
public void ErrorFormat(IFormatProvider provider, string format, params object[] args) {
162+
this.nLogLogger.Error(provider, format, args);
163+
}
164+
165+
public void Fatal(object message) {
166+
this.nLogLogger.Fatal(message);
167+
}
168+
169+
public void Fatal(object message, Exception exception) {
170+
this.nLogLogger.Fatal(String.Format("{0}", message), exception);
171+
}
172+
173+
public void FatalFormat(string format, params object[] args) {
174+
this.nLogLogger.Fatal(CultureInfo.InvariantCulture, format, args);
175+
}
176+
177+
public void FatalFormat(string format, object arg0) {
178+
this.nLogLogger.Fatal(format, arg0);
179+
}
180+
181+
public void FatalFormat(string format, object arg0, object arg1) {
182+
this.nLogLogger.Fatal(format, arg0, arg1);
183+
}
184+
185+
public void FatalFormat(string format, object arg0, object arg1, object arg2) {
186+
this.nLogLogger.Fatal(format, arg0, arg1, arg2);
187+
}
188+
189+
public void FatalFormat(IFormatProvider provider, string format, params object[] args) {
190+
this.nLogLogger.Fatal(provider, format, args);
191+
}
192+
193+
#endregion
194+
195+
/// <summary>
196+
/// Returns a new NLog logger if it exists, or returns null if the assembly cannot be found.
197+
/// </summary>
198+
/// <returns>The created <see cref="ILog"/> instance.</returns>
199+
internal static ILog Initialize(string name) {
200+
try {
201+
return IsNLogPresent ? CreateLogger(name) : null;
202+
} catch (FileLoadException) {
203+
// wrong NLog.dll version
204+
return null;
205+
} catch (TargetInvocationException) {
206+
// Thrown due to some security issues on .NET 4.5.
207+
return null;
208+
} catch (TypeLoadException) {
209+
// Thrown by mono (http://stackoverflow.com/questions/10805773/error-when-pushing-dotnetopenauth-to-staging-or-production-environment)
210+
return null;
211+
}
212+
}
213+
214+
/// <summary>
215+
/// Creates the NLogLogger. Call ONLY after NLog.dll is known to be present.
216+
/// </summary>
217+
/// <returns>The created <see cref="ILog"/> instance.</returns>
218+
private static ILog CreateLogger(string name) {
219+
return new NLogLogger(NLog.LogManager.GetLogger(name));
220+
}
221+
}
222+
}

src/DotNetOpenAuth.Core/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net45" />
77
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" />
88
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
9+
<package id="NLog" version="2.0.0.2000" targetFramework="net40" />
910
<package id="Validation" version="2.0.2.13022" targetFramework="net45" />
1011
</packages>

0 commit comments

Comments
 (0)