Permalink
Browse files

Merge pull request #6 from markswiatek/master

Log4Net changes
  • Loading branch information...
2 parents cc4c858 + 47e8cf1 commit a5429328a7eec227dc32ad2dd908361d51f9a25e @mythz mythz committed Apr 16, 2012
@@ -1,12 +1,12 @@
using System;
-using ServiceStack.Logging;
+using System.IO;
namespace ServiceStack.Logging.Log4Net
{
/// <summary>
/// ILogFactory that creates an Log4Net ILog logger
/// </summary>
- public class Log4NetFactory : ServiceStack.Logging.ILogFactory
+ public class Log4NetFactory : ILogFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="Log4NetFactory"/> class.
@@ -26,6 +26,23 @@ public Log4NetFactory(bool configureLog4Net)
}
/// <summary>
+ /// Initializes a new instance of the <see cref="Log4NetFactory"/> class.
+ /// </summary>
+ /// <param name="log4NetConfigurationFile">The log4 net configuration file to load and watch. If not found configures from App.Config.</param>
+ public Log4NetFactory(string log4NetConfigurationFile)
+ {
+ //Restart logging if necessary
+ log4net.Repository.ILoggerRepository rootRepository = log4net.LogManager.GetRepository();
+ if (rootRepository != null)
+ rootRepository.Shutdown();
+
+ if (File.Exists(log4NetConfigurationFile))
+ log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigurationFile));
+ else
+ log4net.Config.XmlConfigurator.Configure();
+ }
+
+ /// <summary>
/// Gets the logger.
/// </summary>
/// <param name="type">The type.</param>
@@ -1,18 +1,17 @@
using System;
-using ServiceStack.Logging;
namespace ServiceStack.Logging.Log4Net
{
/// <summary>
/// Wrapper over the log4net.1.2.10 and above logger
/// </summary>
- public class Log4NetLogger : ServiceStack.Logging.ILog
+ public class Log4NetLogger : ILog
{
- private readonly log4net.ILog log;
+ private readonly log4net.ILog _log;
public Log4NetLogger(string typeName)
{
- log = log4net.LogManager.GetLogger(typeName);
+ _log = log4net.LogManager.GetLogger(typeName);
}
/// <summary>
@@ -21,18 +20,19 @@ public Log4NetLogger(string typeName)
/// <param name="type">The type.</param>
public Log4NetLogger(Type type)
{
- log = log4net.LogManager.GetLogger(type);
+ _log = log4net.LogManager.GetLogger(type);
}
- public bool IsDebugEnabled { get { return log.IsDebugEnabled; } }
+ public bool IsDebugEnabled { get { return _log.IsDebugEnabled; } }
/// <summary>
/// Logs a Debug message.
/// </summary>
/// <param name="message">The message.</param>
public void Debug(object message)
{
- log.Debug(message);
+ if (_log.IsDebugEnabled)
+ _log.Debug(message);
}
/// <summary>
@@ -42,7 +42,8 @@ public void Debug(object message)
/// <param name="exception">The exception.</param>
public void Debug(object message, Exception exception)
{
- log.Debug(message, exception);
+ if (_log.IsDebugEnabled)
+ _log.Debug(message, exception);
}
/// <summary>
@@ -52,7 +53,8 @@ public void Debug(object message, Exception exception)
/// <param name="args">The args.</param>
public void DebugFormat(string format, params object[] args)
{
- log.DebugFormat(format, args);
+ if (_log.IsDebugEnabled)
+ _log.DebugFormat(format, args);
}
/// <summary>
@@ -61,7 +63,8 @@ public void DebugFormat(string format, params object[] args)
/// <param name="message">The message.</param>
public void Error(object message)
{
- log.Error(message);
+ if (_log.IsErrorEnabled)
+ _log.Error(message);
}
/// <summary>
@@ -71,7 +74,8 @@ public void Error(object message)
/// <param name="exception">The exception.</param>
public void Error(object message, Exception exception)
{
- log.Error(message, exception);
+ if (_log.IsErrorEnabled)
+ _log.Error(message, exception);
}
/// <summary>
@@ -81,7 +85,8 @@ public void Error(object message, Exception exception)
/// <param name="args">The args.</param>
public void ErrorFormat(string format, params object[] args)
{
- log.ErrorFormat(format, args);
+ if (_log.IsErrorEnabled)
+ _log.ErrorFormat(format, args);
}
/// <summary>
@@ -90,7 +95,8 @@ public void ErrorFormat(string format, params object[] args)
/// <param name="message">The message.</param>
public void Fatal(object message)
{
- log.Fatal(message);
+ if (_log.IsFatalEnabled)
+ _log.Fatal(message);
}
/// <summary>
@@ -100,7 +106,8 @@ public void Fatal(object message)
/// <param name="exception">The exception.</param>
public void Fatal(object message, Exception exception)
{
- log.Fatal(message, exception);
+ if (_log.IsFatalEnabled)
+ _log.Fatal(message, exception);
}
/// <summary>
@@ -110,7 +117,8 @@ public void Fatal(object message, Exception exception)
/// <param name="args">The args.</param>
public void FatalFormat(string format, params object[] args)
{
- log.FatalFormat(format, args);
+ if (_log.IsFatalEnabled)
+ _log.FatalFormat(format, args);
}
/// <summary>
@@ -119,7 +127,8 @@ public void FatalFormat(string format, params object[] args)
/// <param name="message">The message.</param>
public void Info(object message)
{
- log.Info(message);
+ if (_log.IsInfoEnabled)
+ _log.Info(message);
}
/// <summary>
@@ -129,7 +138,8 @@ public void Info(object message)
/// <param name="exception">The exception.</param>
public void Info(object message, Exception exception)
{
- log.Info(message, exception);
+ if (_log.IsInfoEnabled)
+ _log.Info(message, exception);
}
/// <summary>
@@ -139,7 +149,8 @@ public void Info(object message, Exception exception)
/// <param name="args">The args.</param>
public void InfoFormat(string format, params object[] args)
{
- log.InfoFormat(format, args);
+ if (_log.IsInfoEnabled)
+ _log.InfoFormat(format, args);
}
/// <summary>
@@ -148,7 +159,8 @@ public void InfoFormat(string format, params object[] args)
/// <param name="message">The message.</param>
public void Warn(object message)
{
- log.Warn(message);
+ if (_log.IsWarnEnabled)
+ _log.Warn(message);
}
/// <summary>
@@ -158,7 +170,8 @@ public void Warn(object message)
/// <param name="exception">The exception.</param>
public void Warn(object message, Exception exception)
{
- log.Warn(message, exception);
+ if (_log.IsWarnEnabled)
+ _log.Warn(message, exception);
}
/// <summary>
@@ -168,7 +181,8 @@ public void Warn(object message, Exception exception)
/// <param name="args">The args.</param>
public void WarnFormat(string format, params object[] args)
{
- log.WarnFormat(format, args);
+ if (_log.IsWarnEnabled)
+ _log.WarnFormat(format, args);
}
}
-}
+}
@@ -158,6 +158,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
+ <Content Include="log4net.Test.config">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
@@ -1,3 +1,4 @@
+using System.IO;
using NUnit.Framework;
using ServiceStack.Logging.Log4Net;
@@ -19,5 +20,18 @@ public void Log4NetFactoryTest()
Assert.IsNotNull(log);
Assert.IsNotNull(log as Log4NetLogger);
}
+
+ [Test]
+ public void Log4NetFactoryTestWithExistingConfigFile()
+ {
+ const string configFile = "log4net.Test.config";
+ Assert.IsTrue(File.Exists(configFile), "Test setup failure. Required log4net config file is missing.");
+
+ Log4NetFactory factory = new Log4NetFactory(configFile);
+
+ ILog log = factory.GetLogger(GetType());
+ Assert.IsNotNull(log);
+ Assert.IsNotNull(log as Log4NetLogger);
+ }
}
}
@@ -0,0 +1,15 @@
+<log4net debug="false">
+ <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
+
+ <!-- ConsoleAppender uses PatternLayout -->
+ <layout type="log4net.Layout.PatternLayout">
+ <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
+ </layout>
+ </appender>
+
+ <!-- Set root logger level to DEBUG and its only appender to DebugFileAppender -->
+ <root>
+ <level value="DEBUG" />
+ </root>
+
+</log4net>

0 comments on commit a542932

Please sign in to comment.