logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1166079 - in /logging/log4net/trunk: src/Appender/FileAppender.cs tests/src/Appender/RollingFileAppenderTest.cs
Date Wed, 07 Sep 2011 09:23:52 GMT
Author: bodewig
Date: Wed Sep  7 09:23:52 2011
New Revision: 1166079

URL: http://svn.apache.org/viewvc?rev=1166079&view=rev
Log:
test new MutexLock and make it actually work.  LOG4NET-164

Modified:
    logging/log4net/trunk/src/Appender/FileAppender.cs
    logging/log4net/trunk/tests/src/Appender/RollingFileAppenderTest.cs

Modified: logging/log4net/trunk/src/Appender/FileAppender.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/FileAppender.cs?rev=1166079&r1=1166078&r2=1166079&view=diff
==============================================================================
--- logging/log4net/trunk/src/Appender/FileAppender.cs (original)
+++ logging/log4net/trunk/src/Appender/FileAppender.cs Wed Sep  7 09:23:52 2011
@@ -388,7 +388,7 @@ namespace log4net.Appender
                     }
 
                     FileMode fileOpenMode = append ? FileMode.Append : FileMode.Create;
-                    return new FileStream(filename, fileOpenMode, FileAccess.Write, FileShare.Read);
+                    return new FileStream(filename, fileOpenMode, FileAccess.Write, fileShare);
                 }
             }
 
@@ -640,12 +640,15 @@ namespace log4net.Appender
             /// </remarks>
             public override void CloseFile()
             {
-                CloseStream(m_stream);
-                m_stream = null;
-
-                m_mutex.ReleaseMutex();
-                m_mutex.Close();
-                m_mutexClosed = true;
+                try {
+                    CloseStream(m_stream);
+                    m_stream = null;
+                }
+                finally {
+                    m_mutex.ReleaseMutex();
+                    m_mutex.Close();
+                    m_mutexClosed = true;
+                }
             }
 
             /// <summary>
@@ -659,13 +662,14 @@ namespace log4net.Appender
             /// </remarks>
             public override Stream AcquireLock()
             {
-                // TODO: add timeout?
-                m_mutex.WaitOne();
-
-                // should always be true (and fast) for FileStream
-                if (m_stream.CanSeek)
-                {
-                    m_stream.Seek(0, SeekOrigin.End);
+                if (m_mutex != null) {
+                    // TODO: add timeout?
+                    m_mutex.WaitOne();
+
+                    // should always be true (and fast) for FileStream
+                    if (m_stream.CanSeek) {
+                        m_stream.Seek(0, SeekOrigin.End);
+                    }
                 }
 
                 return m_stream;
@@ -676,7 +680,7 @@ namespace log4net.Appender
             /// </summary>
             public override void ReleaseLock()
             {
-                if (m_mutexClosed == false)
+                if (m_mutexClosed == false && m_mutex != null)
                 {
                     m_mutex.ReleaseMutex();
                 }

Modified: logging/log4net/trunk/tests/src/Appender/RollingFileAppenderTest.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/RollingFileAppenderTest.cs?rev=1166079&r1=1166078&r2=1166079&view=diff
==============================================================================
--- logging/log4net/trunk/tests/src/Appender/RollingFileAppenderTest.cs (original)
+++ logging/log4net/trunk/tests/src/Appender/RollingFileAppenderTest.cs Wed Sep  7 09:23:52
2011
@@ -1603,7 +1603,74 @@ namespace log4net.Tests.Appender
 			Assert.AreEqual("", sh.Message, "Unexpected error message");
 		}
 
-		/// <summary>
+        /// <summary>
+        /// Verifies that attempting to log to a locked file fails gracefully
+        /// </summary>
+        [Test]
+        public void TestMutexLockFails() {
+            String filename = "test.log";
+
+            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
+            fs.Write(Encoding.ASCII.GetBytes("Test"), 0, 4);
+
+            SilentErrorHandler sh = new SilentErrorHandler();
+            ILogger log = CreateLogger(filename, new FileAppender.MutexLock(), sh);
+            log.Log(GetType(), Level.Info, "This is a message", null);
+            log.Log(GetType(), Level.Info, "This is a message 2", null);
+            DestroyLogger();
+            fs.Close();
+
+            AssertFileEquals(filename, "Test");
+            Assert.AreEqual("Unable to acquire lock on file", sh.Message.Substring(0, 30),
"Expecting an error message");
+        }
+
+        /// <summary>
+        /// Verifies that attempting to log to a locked file recovers if the lock is released
+        /// </summary>
+        [Test]
+        public void TestMutexLockRecovers() {
+            String filename = "test.log";
+
+            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
+            fs.Write(Encoding.ASCII.GetBytes("Test"), 0, 4);
+
+            SilentErrorHandler sh = new SilentErrorHandler();
+            ILogger log = CreateLogger(filename, new FileAppender.MutexLock(), sh);
+            log.Log(GetType(), Level.Info, "This is a message", null);
+            fs.Close();
+            log.Log(GetType(), Level.Info, "This is a message 2", null);
+            DestroyLogger();
+
+            AssertFileEquals(filename, "This is a message 2" + Environment.NewLine);
+            Assert.AreEqual("Unable to acquire lock on file", sh.Message.Substring(0, 30),
"Expecting an error message");
+        }
+
+        /// <summary>
+        /// Verifies that attempting to log to a file with ExclusiveLock really locks the
file
+        /// </summary>
+        [Test]
+        public void TestMutexLockUnlocks() {
+            String filename = "test.log";
+            bool locked;
+
+            SilentErrorHandler sh = new SilentErrorHandler();
+            ILogger log = CreateLogger(filename, new FileAppender.MutexLock(), sh);
+            log.Log(GetType(), Level.Info, "This is a message", null);
+
+            locked = true;
+            FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
+            fs.Write(Encoding.ASCII.GetBytes("Test" + Environment.NewLine), 0, 4 + Environment.NewLine.Length);
+            fs.Close();
+
+            log.Log(GetType(), Level.Info, "This is a message 2", null);
+            DestroyLogger();
+
+            Assert.IsTrue(locked, "File was not locked");
+            AssertFileEquals(filename, "This is a message" + Environment.NewLine + "Test"
+ Environment.NewLine + "This is a message 2" + Environment.NewLine);
+            Assert.AreEqual("", sh.Message, "Unexpected error message");
+        }
+
+        /// <summary>
 		/// Verify that the default LockModel is ExclusiveLock, to maintain backwards compatibility
with previous behaviour
 		/// </summary>
 		[Test]



Mime
View raw message