Propchange: logging/log4net/trunk/src/Util/ILogExtensions.cs ------------------------------------------------------------------------------ svn:eol-style = native
Modified: logging/log4net/trunk/tests/src/Appender/AdoNetAppenderTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/AdoNetAppenderTest.cs?rev=1764545&r1=1764544&r2=1764545&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Appender/AdoNetAppenderTest.cs (original) +++ logging/log4net/trunk/tests/src/Appender/AdoNetAppenderTest.cs Wed Oct 12 20:38:35 2016 @@ -51,30 +51,30 @@ namespace log4net.Tests.Appender ILog log = LogManager.GetLogger(rep.Name, "NoBufferingTest"); log.Debug("Message"); Assert.AreEqual(1, Log4NetCommand.MostRecentInstance.ExecuteNonQueryCount); - } - - [Test] - public void BufferingTest() - { - ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); - - int bufferSize = 5; - - AdoNetAppender adoNetAppender = new AdoNetAppender(); - adoNetAppender.BufferSize = bufferSize; - adoNetAppender.ConnectionType = "log4net.Tests.Appender.AdoNet.Log4NetConnection"; - adoNetAppender.ActivateOptions(); - - BasicConfigurator.Configure(rep, adoNetAppender); - - ILog log = LogManager.GetLogger(rep.Name, "BufferingTest"); - for (int i = 0; i < bufferSize; i++) - { - log.Debug("Message"); - Assert.IsNull(Log4NetCommand.MostRecentInstance); - } - log.Debug("Message"); - Assert.AreEqual(bufferSize+1, Log4NetCommand.MostRecentInstance.ExecuteNonQueryCount); + } + + [Test] + public void BufferingTest() + { + ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); + + int bufferSize = 5; + + AdoNetAppender adoNetAppender = new AdoNetAppender(); + adoNetAppender.BufferSize = bufferSize; + adoNetAppender.ConnectionType = "log4net.Tests.Appender.AdoNet.Log4NetConnection"; + adoNetAppender.ActivateOptions(); + + BasicConfigurator.Configure(rep, adoNetAppender); + + ILog log = LogManager.GetLogger(rep.Name, "BufferingTest"); + for (int i = 0; i < bufferSize; i++) + { + log.Debug("Message"); + Assert.IsNull(Log4NetCommand.MostRecentInstance); + } + log.Debug("Message"); + Assert.AreEqual(bufferSize+1, Log4NetCommand.MostRecentInstance.ExecuteNonQueryCount); } [Test] @@ -164,13 +164,13 @@ namespace log4net.Tests.Appender param = (IDbDataParameter)command.Parameters["@exception"]; Assert.IsEmpty((string)param.Value); - } - - [Test] - public void BufferingWebsiteExample() - { - XmlDocument log4netConfig = new XmlDocument(); - #region Load log4netConfig + } + + [Test] + public void BufferingWebsiteExample() + { + XmlDocument log4netConfig = new XmlDocument(); + #region Load log4netConfig log4netConfig.LoadXml(@" <log4net> <appender name=""AdoNetAppender"" type=""log4net.Appender.AdoNetAppender""> @@ -226,37 +226,37 @@ namespace log4net.Tests.Appender <level value=""ALL"" /> <appender-ref ref=""AdoNetAppender"" /> </root> - </log4net>"); - #endregion - - ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); - XmlConfigurator.Configure(rep, log4netConfig["log4net"]); - ILog log = LogManager.GetLogger(rep.Name, "WebsiteExample"); - - for (int i = 0; i < 3; i++) - { - log.Debug("Message"); - } - - IDbCommand command = Log4NetCommand.MostRecentInstance; - - Assert.AreEqual( - "INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)", - command.CommandText); - - Assert.AreEqual(6, command.Parameters.Count); - - IDbDataParameter param = (IDbDataParameter)command.Parameters["@message"]; - Assert.AreEqual("Message", param.Value); - - param = (IDbDataParameter)command.Parameters["@log_level"]; - Assert.AreEqual(Level.Debug.ToString(), param.Value); - - param = (IDbDataParameter)command.Parameters["@logger"]; - Assert.AreEqual("WebsiteExample", param.Value); - - param = (IDbDataParameter)command.Parameters["@exception"]; - Assert.IsEmpty((string)param.Value); + </log4net>"); + #endregion + + ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); + XmlConfigurator.Configure(rep, log4netConfig["log4net"]); + ILog log = LogManager.GetLogger(rep.Name, "WebsiteExample"); + + for (int i = 0; i < 3; i++) + { + log.Debug("Message"); + } + + IDbCommand command = Log4NetCommand.MostRecentInstance; + + Assert.AreEqual( + "INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)", + command.CommandText); + + Assert.AreEqual(6, command.Parameters.Count); + + IDbDataParameter param = (IDbDataParameter)command.Parameters["@message"]; + Assert.AreEqual("Message", param.Value); + + param = (IDbDataParameter)command.Parameters["@log_level"]; + Assert.AreEqual(Level.Debug.ToString(), param.Value); + + param = (IDbDataParameter)command.Parameters["@logger"]; + Assert.AreEqual("WebsiteExample", param.Value); + + param = (IDbDataParameter)command.Parameters["@exception"]; + Assert.IsEmpty((string)param.Value); } [Test] Modified: logging/log4net/trunk/tests/src/Appender/AppenderCollectionTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/AppenderCollectionTest.cs?rev=1764545&r1=1764544&r2=1764545&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Appender/AppenderCollectionTest.cs (original) +++ logging/log4net/trunk/tests/src/Appender/AppenderCollectionTest.cs Wed Oct 12 20:38:35 2016 @@ -1,65 +1,65 @@ -#region Apache License -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to you under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using log4net.Appender; -using NUnit.Framework; - -namespace log4net.Tests.Appender -{ - /// <summary> - /// Used for internal unit testing the <see cref="AppenderCollection"/> class. - /// </summary> - /// <remarks> - /// Used for internal unit testing the <see cref="AppenderCollection"/> class. - /// </remarks> - /// <author>Carlos Muñoz</author> - [TestFixture] - public class AppenderCollectionTest - { - /// <summary> - /// Verifies that ToArray returns the elements of the <see cref="AppenderCollection"/> - /// </summary> - [Test] - public void ToArrayTest() - { - AppenderCollection appenderCollection = new AppenderCollection(); - IAppender appender = new MemoryAppender(); - appenderCollection.Add(appender); - - IAppender[] appenderArray = appenderCollection.ToArray(); - - Assert.AreEqual(1, appenderArray.Length); - Assert.AreEqual(appender, appenderArray[0]); - } - - [Test] - public void ReadOnlyToArrayTest() - { - AppenderCollection appenderCollection = new AppenderCollection(); - IAppender appender = new MemoryAppender(); - appenderCollection.Add(appender); - AppenderCollection readonlyAppenderCollection = AppenderCollection.ReadOnly(appenderCollection); - - IAppender[] appenderArray = readonlyAppenderCollection.ToArray(); - - Assert.AreEqual(1, appenderArray.Length); - Assert.AreEqual(appender, appenderArray[0]); - } - } +#region Apache License +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to you under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using log4net.Appender; +using NUnit.Framework; + +namespace log4net.Tests.Appender +{ + /// <summary> + /// Used for internal unit testing the <see cref="AppenderCollection"/> class. + /// </summary> + /// <remarks> + /// Used for internal unit testing the <see cref="AppenderCollection"/> class. + /// </remarks> + /// <author>Carlos Muñoz</author> + [TestFixture] + public class AppenderCollectionTest + { + /// <summary> + /// Verifies that ToArray returns the elements of the <see cref="AppenderCollection"/> + /// </summary> + [Test] + public void ToArrayTest() + { + AppenderCollection appenderCollection = new AppenderCollection(); + IAppender appender = new MemoryAppender(); + appenderCollection.Add(appender); + + IAppender[] appenderArray = appenderCollection.ToArray(); + + Assert.AreEqual(1, appenderArray.Length); + Assert.AreEqual(appender, appenderArray[0]); + } + + [Test] + public void ReadOnlyToArrayTest() + { + AppenderCollection appenderCollection = new AppenderCollection(); + IAppender appender = new MemoryAppender(); + appenderCollection.Add(appender); + AppenderCollection readonlyAppenderCollection = AppenderCollection.ReadOnly(appenderCollection); + + IAppender[] appenderArray = readonlyAppenderCollection.ToArray(); + + Assert.AreEqual(1, appenderArray.Length); + Assert.AreEqual(appender, appenderArray[0]); + } + } } \ No newline at end of file Propchange: logging/log4net/trunk/tests/src/Appender/AppenderCollectionTest.cs ------------------------------------------------------------------------------ svn:eol-style = native Modified: logging/log4net/trunk/tests/src/Appender/RemotingAppenderTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/RemotingAppenderTest.cs?rev=1764545&r1=1764544&r2=1764545&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Appender/RemotingAppenderTest.cs (original) +++ logging/log4net/trunk/tests/src/Appender/RemotingAppenderTest.cs Wed Oct 12 20:38:35 2016 @@ -201,15 +201,15 @@ namespace log4net.Tests.Appender private void RegisterRemotingServerChannel() { if (m_remotingChannel == null) - { - BinaryClientFormatterSinkProvider clientSinkProvider = new BinaryClientFormatterSinkProvider(); - - BinaryServerFormatterSinkProvider serverSinkProvider = new BinaryServerFormatterSinkProvider(); - serverSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; - - Hashtable channelProperties = new Hashtable(); - channelProperties["port"] = 8085; - + { + BinaryClientFormatterSinkProvider clientSinkProvider = new BinaryClientFormatterSinkProvider(); + + BinaryServerFormatterSinkProvider serverSinkProvider = new BinaryServerFormatterSinkProvider(); + serverSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; + + Hashtable channelProperties = new Hashtable(); + channelProperties["port"] = 8085; + m_remotingChannel = new TcpChannel(channelProperties, clientSinkProvider, serverSinkProvider); // Setup remoting server try Modified: logging/log4net/trunk/tests/src/Appender/SmtpPickupDirAppenderTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/SmtpPickupDirAppenderTest.cs?rev=1764545&r1=1764544&r2=1764545&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Appender/SmtpPickupDirAppenderTest.cs (original) +++ logging/log4net/trunk/tests/src/Appender/SmtpPickupDirAppenderTest.cs Wed Oct 12 20:38:35 2016 @@ -1,249 +1,249 @@ -#region Apache License -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to you under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using System; -using System.Collections; -using System.Diagnostics; -using System.IO; -using System.Text; - -using log4net.Appender; -using log4net.Core; -using log4net.Layout; - -using NUnit.Framework; - -namespace log4net.Tests.Appender -{ - /// <summary> - /// Used for internal unit testing the <see cref="SmtpPickupDirAppender"/> class. - /// </summary> - [TestFixture] - public class SmtpPickupDirAppenderTest - { - private readonly string _testPickupDir; - - private class SilentErrorHandler : IErrorHandler - { - private StringBuilder m_buffer = new StringBuilder(); - - public string Message - { - get { return m_buffer.ToString(); } - } - - public void Error(string message) - { - m_buffer.Append(message + "\n"); - } - - public void Error(string message, Exception e) - { - m_buffer.Append(message + "\n" + e.Message + "\n"); - } - - public void Error(string message, Exception e, ErrorCode errorCode) - { - m_buffer.Append(message + "\n" + e.Message + "\n"); - } - } - - public SmtpPickupDirAppenderTest() - { - _testPickupDir = Path.Combine(Directory.GetCurrentDirectory(), "SmtpPickupDirAppenderTest_PickupDir"); - } - /// <summary> - /// Sets up variables used for the tests - /// </summary> - private void InitializePickupDir() - { - Directory.CreateDirectory(_testPickupDir); - } - - /// <summary> - /// Shuts down any loggers in the hierarchy, along - /// with all appenders, and deletes any test files used - /// for logging. - /// </summary> - private void ResetLogger() - { - // Regular users should not use the clear method lightly! - Utils.GetRepository().ResetConfiguration(); - Utils.GetRepository().Shutdown(); - ((Repository.Hierarchy.Hierarchy)Utils.GetRepository()).Clear(); - } - - /// <summary> - /// Any initialization that happens before each test can - /// go here - /// </summary> - [SetUp] - public void SetUp() - { - ResetLogger(); - DeleteTestFiles(); - InitializePickupDir(); - } - - /// <summary> - /// Any steps that happen after each test go here - /// </summary> - [TearDown] - public void TearDown() - { - ResetLogger(); - DeleteTestFiles(); - } - - /// <summary> - /// Removes all test files that exist - /// </summary> - private void DeleteTestFiles() - { - if (Directory.Exists(_testPickupDir)) - { - Directory.Delete(_testPickupDir, true); - } - } - - /// <summary> - /// Creates a logger hierarchy, configures a SMTP pickup dir appender and returns an ILogger - /// </summary> - /// <param name="appender">The appender to use</param> - /// <returns>A configured ILogger</returns> - private ILogger CreateLogger(SmtpPickupDirAppender appender) - { - Repository.Hierarchy.Hierarchy h = (Repository.Hierarchy.Hierarchy)LogManager.CreateRepository("TestRepository"); - - PatternLayout layout = new PatternLayout(); - layout.ConversionPattern = "%m%n"; - layout.ActivateOptions(); - - appender.Layout = layout; - appender.ActivateOptions(); - - h.Root.AddAppender(appender); - h.Configured = true; - - ILogger log = h.GetLogger("Logger"); - return log; - } - - /// <summary> - /// Create an appender to use for the logger - /// </summary> - /// <param name="handler">The error handler to use.</param> - /// <returns></returns> - private SmtpPickupDirAppender CreateSmtpPickupDirAppender(IErrorHandler handler) - { - SmtpPickupDirAppender appender = new SmtpPickupDirAppender(); - appender.PickupDir = _testPickupDir; - appender.ErrorHandler = handler; - return appender; - } - - /// <summary> - /// Destroys the logger hierarchy created by <see cref="SmtpPickupDirAppenderTest.CreateLogger"/> - /// </summary> - private static void DestroyLogger() - { - Repository.Hierarchy.Hierarchy h = (Repository.Hierarchy.Hierarchy)LogManager.GetRepository("TestRepository"); - h.ResetConfiguration(); - //Replace the repository selector so that we can recreate the hierarchy with the same name if necessary - LoggerManager.RepositorySelector = new DefaultRepositorySelector(typeof(log4net.Repository.Hierarchy.Hierarchy)); - } - - /// <summary> - /// Tests if the sent message contained the date header. - /// </summary> - [Test] - public void TestOutputContainsSentDate() - { - SilentErrorHandler sh = new SilentErrorHandler(); - SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); - ILogger log = CreateLogger(appender); - log.Log(GetType(), Level.Info, "This is a message", null); - log.Log(GetType(), Level.Info, "This is a message 2", null); - DestroyLogger(); - - Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); - string[] fileContent = File.ReadAllLines((Directory.GetFiles(_testPickupDir)[0])); - bool hasDateHeader = false; - const string dateHeaderStart = "Date: "; - foreach (string line in fileContent) - { - if(line.StartsWith(dateHeaderStart)) - { - var datePart = line.Substring(dateHeaderStart.Length); - var date = DateTime.ParseExact(datePart, "r", System.Globalization.CultureInfo.InvariantCulture); - var diff = Math.Abs( (DateTime.UtcNow - date).TotalMilliseconds); - Assert.LessOrEqual(diff, 1000, "Times should be equal, allowing a diff of one second to make test robust"); - hasDateHeader = true; - } - } - Assert.IsTrue(hasDateHeader, "Output must contains a date header"); - - Assert.AreEqual("", sh.Message, "Unexpected error message"); - } - - /// <summary> - /// Verifies that file extension is applied to output file name. - /// </summary> - [Test] - public void TestConfigurableFileExtension() - { - const string fileExtension = "eml"; - SilentErrorHandler sh = new SilentErrorHandler(); - SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); - appender.FileExtension = fileExtension; - ILogger log = CreateLogger(appender); - log.Log(GetType(), Level.Info, "This is a message", null); - log.Log(GetType(), Level.Info, "This is a message 2", null); - DestroyLogger(); - - Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); - var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); - Assert.AreEqual("." + fileExtension, fileInfo.Extension); - Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)); }); // Assert that filename before extension is a guid - - Assert.AreEqual("", sh.Message, "Unexpected error message"); - } - - /// <summary> - /// Verifies that logging a message actually produces output - /// </summary> - [Test] - public void TestDefaultFileNameIsAGuid() - { - SilentErrorHandler sh = new SilentErrorHandler(); - SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); - ILogger log = CreateLogger(appender); - log.Log(GetType(), Level.Info, "This is a message", null); - log.Log(GetType(), Level.Info, "This is a message 2", null); - DestroyLogger(); - - Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); - var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); - Assert.IsEmpty(fileInfo.Extension); - Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name); }); // Assert that filename is a guid - - Assert.AreEqual("", sh.Message, "Unexpected error message"); - } - } +#region Apache License +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to you under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using System; +using System.Collections; +using System.Diagnostics; +using System.IO; +using System.Text; + +using log4net.Appender; +using log4net.Core; +using log4net.Layout; + +using NUnit.Framework; + +namespace log4net.Tests.Appender +{ + /// <summary> + /// Used for internal unit testing the <see cref="SmtpPickupDirAppender"/> class. + /// </summary> + [TestFixture] + public class SmtpPickupDirAppenderTest + { + private readonly string _testPickupDir; + + private class SilentErrorHandler : IErrorHandler + { + private StringBuilder m_buffer = new StringBuilder(); + + public string Message + { + get { return m_buffer.ToString(); } + } + + public void Error(string message) + { + m_buffer.Append(message + "\n"); + } + + public void Error(string message, Exception e) + { + m_buffer.Append(message + "\n" + e.Message + "\n"); + } + + public void Error(string message, Exception e, ErrorCode errorCode) + { + m_buffer.Append(message + "\n" + e.Message + "\n"); + } + } + + public SmtpPickupDirAppenderTest() + { + _testPickupDir = Path.Combine(Directory.GetCurrentDirectory(), "SmtpPickupDirAppenderTest_PickupDir"); + } + /// <summary> + /// Sets up variables used for the tests + /// </summary> + private void InitializePickupDir() + { + Directory.CreateDirectory(_testPickupDir); + } + + /// <summary> + /// Shuts down any loggers in the hierarchy, along + /// with all appenders, and deletes any test files used + /// for logging. + /// </summary> + private void ResetLogger() + { + // Regular users should not use the clear method lightly! + Utils.GetRepository().ResetConfiguration(); + Utils.GetRepository().Shutdown(); + ((Repository.Hierarchy.Hierarchy)Utils.GetRepository()).Clear(); + } + + /// <summary> + /// Any initialization that happens before each test can + /// go here + /// </summary> + [SetUp] + public void SetUp() + { + ResetLogger(); + DeleteTestFiles(); + InitializePickupDir(); + } + + /// <summary> + /// Any steps that happen after each test go here + /// </summary> + [TearDown] + public void TearDown() + { + ResetLogger(); + DeleteTestFiles(); + } + + /// <summary> + /// Removes all test files that exist + /// </summary> + private void DeleteTestFiles() + { + if (Directory.Exists(_testPickupDir)) + { + Directory.Delete(_testPickupDir, true); + } + } + + /// <summary> + /// Creates a logger hierarchy, configures a SMTP pickup dir appender and returns an ILogger + /// </summary> + /// <param name="appender">The appender to use</param> + /// <returns>A configured ILogger</returns> + private ILogger CreateLogger(SmtpPickupDirAppender appender) + { + Repository.Hierarchy.Hierarchy h = (Repository.Hierarchy.Hierarchy)LogManager.CreateRepository("TestRepository"); + + PatternLayout layout = new PatternLayout(); + layout.ConversionPattern = "%m%n"; + layout.ActivateOptions(); + + appender.Layout = layout; + appender.ActivateOptions(); + + h.Root.AddAppender(appender); + h.Configured = true; + + ILogger log = h.GetLogger("Logger"); + return log; + } + + /// <summary> + /// Create an appender to use for the logger + /// </summary> + /// <param name="handler">The error handler to use.</param> + /// <returns></returns> + private SmtpPickupDirAppender CreateSmtpPickupDirAppender(IErrorHandler handler) + { + SmtpPickupDirAppender appender = new SmtpPickupDirAppender(); + appender.PickupDir = _testPickupDir; + appender.ErrorHandler = handler; + return appender; + } + + /// <summary> + /// Destroys the logger hierarchy created by <see cref="SmtpPickupDirAppenderTest.CreateLogger"/> + /// </summary> + private static void DestroyLogger() + { + Repository.Hierarchy.Hierarchy h = (Repository.Hierarchy.Hierarchy)LogManager.GetRepository("TestRepository"); + h.ResetConfiguration(); + //Replace the repository selector so that we can recreate the hierarchy with the same name if necessary + LoggerManager.RepositorySelector = new DefaultRepositorySelector(typeof(log4net.Repository.Hierarchy.Hierarchy)); + } + + /// <summary> + /// Tests if the sent message contained the date header. + /// </summary> + [Test] + public void TestOutputContainsSentDate() + { + SilentErrorHandler sh = new SilentErrorHandler(); + SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); + ILogger log = CreateLogger(appender); + log.Log(GetType(), Level.Info, "This is a message", null); + log.Log(GetType(), Level.Info, "This is a message 2", null); + DestroyLogger(); + + Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); + string[] fileContent = File.ReadAllLines((Directory.GetFiles(_testPickupDir)[0])); + bool hasDateHeader = false; + const string dateHeaderStart = "Date: "; + foreach (string line in fileContent) + { + if(line.StartsWith(dateHeaderStart)) + { + var datePart = line.Substring(dateHeaderStart.Length); + var date = DateTime.ParseExact(datePart, "r", System.Globalization.CultureInfo.InvariantCulture); + var diff = Math.Abs( (DateTime.UtcNow - date).TotalMilliseconds); + Assert.LessOrEqual(diff, 1000, "Times should be equal, allowing a diff of one second to make test robust"); + hasDateHeader = true; + } + } + Assert.IsTrue(hasDateHeader, "Output must contains a date header"); + + Assert.AreEqual("", sh.Message, "Unexpected error message"); + } + + /// <summary> + /// Verifies that file extension is applied to output file name. + /// </summary> + [Test] + public void TestConfigurableFileExtension() + { + const string fileExtension = "eml"; + SilentErrorHandler sh = new SilentErrorHandler(); + SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); + appender.FileExtension = fileExtension; + ILogger log = CreateLogger(appender); + log.Log(GetType(), Level.Info, "This is a message", null); + log.Log(GetType(), Level.Info, "This is a message 2", null); + DestroyLogger(); + + Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); + var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); + Assert.AreEqual("." + fileExtension, fileInfo.Extension); + Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)); }); // Assert that filename before extension is a guid + + Assert.AreEqual("", sh.Message, "Unexpected error message"); + } + + /// <summary> + /// Verifies that logging a message actually produces output + /// </summary> + [Test] + public void TestDefaultFileNameIsAGuid() + { + SilentErrorHandler sh = new SilentErrorHandler(); + SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); + ILogger log = CreateLogger(appender); + log.Log(GetType(), Level.Info, "This is a message", null); + log.Log(GetType(), Level.Info, "This is a message 2", null); + DestroyLogger(); + + Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); + var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); + Assert.IsEmpty(fileInfo.Extension); + Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name); }); // Assert that filename is a guid + + Assert.AreEqual("", sh.Message, "Unexpected error message"); + } + } } \ No newline at end of file Propchange: logging/log4net/trunk/tests/src/Appender/SmtpPickupDirAppenderTest.cs ------------------------------------------------------------------------------ svn:eol-style = native