Exemplo n.º 1
0
        virtual protected void OpenFile(string fileName, bool append)
        {
            lock (this)
            {
                Reset();


                m_fileName     = fileName;
                m_appendToFile = append;

                LockingModel.CurrentHandler = this;
                LockingModel.OpenFile(fileName, append, m_encoding);
                m_stream = new LockingStream(LockingModel);

                if (m_stream != null)
                {
                    m_stream.AcquireLock();
                    try
                    {
                        SetQWForFiles(new StreamWriter(m_stream, m_encoding));
                    }
                    finally
                    {
                        m_stream.ReleaseLock();
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets and <i>opens</i> the file where the log output will go. The specified file must be writable.
        /// </summary>
        /// <param name="fileName">The path to the log file. Must be a fully qualified path.</param>
        /// <param name="append">If true will append to fileName. Otherwise will truncate fileName</param>
        /// <remarks>
        /// <para>
        /// If there was already an opened file, then the previous file
        /// is closed first.
        /// </para>
        /// <para>
        /// This method will ensure that the directory structure
        /// for the <paramref name="fileName"/> specified exists.
        /// </para>
        /// </remarks>
        virtual protected void OpenFile(string fileName, bool append)
        {
            if (LogLog.IsErrorEnabled)
            {
                // Internal check that the fileName passed in is a rooted path
                bool isPathRooted;
                using (SecurityContext.Impersonate(this))
                {
                    isPathRooted = Path.IsPathRooted(fileName);
                }
                if (!isPathRooted)
                {
                    LogLog.Error(typeof(FileAppender), "FileAppender: INTERNAL ERROR. OpenFile(" + fileName + "): File name is not fully qualified.");
                }
            }

            lock (this)
            {
                Reset();

                LogLog.Debug(typeof(FileAppender), "FileAppender: Opening file for writing [" + fileName + "] append [" + append + "]");

                // Save these for later, allowing retries if file open fails
                m_fileName     = fileName;
                m_appendToFile = append;

                int lockLevel = 0;

                if (m_stream != null)
                {
                    lockLevel = m_stream.LockLevel;
                }
                LockingModel.CurrentAppender = this;
                LockingModel.OpenFile(fileName, append, m_encoding);
                m_stream = new LockingStream(LockingModel, lockLevel);

                if (m_stream != null)
                {
                    LockingStream theStream = m_stream;

                    if (theStream.AcquireLock())
                    {
                        try
                        {
                            SetQWForFiles(new StreamWriter(m_stream, m_encoding));
                        }
                        finally
                        {
                            theStream.ReleaseLock();
                        }
                    }
                }

                WriteHeader();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 打开文件为写日志操作做准备
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="append">是否添加日志信息到文件尾</param>
        virtual protected void OpenFile(string fileName, bool append)
        {
            lock (this)
            {
                //先进行关闭之前打开的文件
                Reset();

                // 保存这些后, 允许重试如果打开文件失败
                m_fileName     = fileName;
                m_appendToFile = append;

                switch (LogConfig.Instance.LockType)
                {
                //独占
                case LockingType.Exclusive:
                    m_lockingModel = new ExclusiveLock();
                    break;

                //跨进程文件锁
                case LockingType.InterProcess:
                    m_lockingModel = new InterProcessLock();
                    break;

                //最小时间
                case LockingType.Minimal:
                    m_lockingModel = new MinimalLock();
                    break;
                }
                //
                LockingModel.CurrentAppender = this;
                //打开文件
                LockingModel.OpenFile(fileName, append, m_encoding);

                m_stream = new LockingStream(LockingModel);

                if (m_stream != null)
                {
                    m_stream.AcquireLock();

                    try
                    {
                        SetQWForFiles(new StreamWriter(m_stream, m_encoding));
                    }
                    finally
                    {
                        m_stream.ReleaseLock();
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method is called by the <see cref="AppenderSkeleton.DoAppend(LoggingEvent[])"/>
        /// method.
        /// </summary>
        /// <param name="loggingEvents">The array of events to log.</param>
        /// <remarks>
        /// <para>
        /// Acquires the output file locks once before writing all the events to
        /// the stream.
        /// </para>
        /// </remarks>
        override protected void Append(LoggingEvent[] loggingEvents)
        {
            LockingStream theStream = m_stream;

            if (theStream.AcquireLock())
            {
                try
                {
                    base.Append(loggingEvents);
                }
                finally
                {
                    theStream.ReleaseLock();
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets and <i>opens</i> the file where the log output will go. The specified file must be writable.
        /// </summary>
        /// <param name="fileName">The path to the log file. Must be a fully qualified path.</param>
        /// <param name="append">If true will append to fileName. Otherwise will truncate fileName</param>
        /// <remarks>
        /// <para>
        /// If there was already an opened file, then the previous file
        /// is closed first.
        /// </para>
        /// <para>
        /// This method will ensure that the directory structure
        /// for the <paramref name="fileName"/> specified exists.
        /// </para>
        /// </remarks>
        virtual protected void OpenFile(string fileName, bool append)
        {
            if (LogLog.IsErrorEnabled)
            {
                // Internal check that the fileName passed in is a rooted path
                bool isPathRooted = false;

                isPathRooted = Path.IsPathRooted(fileName);

                if (!isPathRooted)
                {
                    LogLog.Error(declaringType, "INTERNAL ERROR. OpenFile(" + fileName + "): File name is not fully qualified.");
                }
            }

            lock (this)
            {
                Reset();

                LogLog.Debug(declaringType, "Opening file for writing [" + fileName + "] append [" + append + "]");

                // Save these for later, allowing retries if file open fails
                m_fileName     = fileName;
                m_appendToFile = append;

                LockingModel.CurrentAppender = this;
                LockingModel.OpenFile(fileName, append, m_encoding);
                m_stream = new LockingStream(LockingModel);

                if (m_stream != null)
                {
                    m_stream.AcquireLock();
                    try
                    {
                        SetQWForFiles(new StreamWriter(m_stream, m_encoding));
                    }
                    finally
                    {
                        m_stream.ReleaseLock();
                    }
                }

                WriteHeader();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Closes the underlying <see cref="TextWriter"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Closes the underlying <see cref="TextWriter"/>.
        /// </para>
        /// </remarks>
        protected override void CloseWriter()
        {
            if (m_stream != null)
            {
                LockingStream theStream = m_stream;

                if (theStream.AcquireLock())
                {
                    try
                    {
                        base.CloseWriter();
                    }
                    finally
                    {
                        theStream.ReleaseLock();
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Writes a footer as produced by the embedded layout's <see cref="ILayout.Footer"/> property.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Writes a footer as produced by the embedded layout's <see cref="ILayout.Footer"/> property.
        /// </para>
        /// </remarks>
        protected override void WriteFooter()
        {
            if (m_stream != null)
            {
                //WriteFooter can be called even before a file is opened
                LockingStream theStream = m_stream;

                if (theStream.AcquireLock())
                {
                    try
                    {
                        base.WriteFooter();
                    }
                    finally
                    {
                        theStream.ReleaseLock();
                    }
                }
            }
        }
Exemplo n.º 8
0
 protected virtual void OpenFile(string fileName, bool append)
 {
     if (LogLog.IsErrorEnabled)
     {
         bool flag = false;
         using (this.SecurityContext.Impersonate(this))
         {
             flag = Path.IsPathRooted(fileName);
         }
         if (!flag)
         {
             LogLog.Error(declaringType, "INTERNAL ERROR. OpenFile(" + fileName + "): File name is not fully qualified.");
         }
     }
     lock (this)
     {
         this.Reset();
         object[] objArray1 = new object[] { "Opening file for writing [", fileName, "] append [", append, "]" };
         LogLog.Debug(declaringType, string.Concat(objArray1));
         this.m_fileName     = fileName;
         this.m_appendToFile = append;
         this.LockingModel.CurrentAppender = this;
         this.LockingModel.OpenFile(fileName, append, this.m_encoding);
         this.m_stream = new LockingStream(this.LockingModel);
         if (this.m_stream != null)
         {
             this.m_stream.AcquireLock();
             try
             {
                 this.SetQWForFiles(new StreamWriter(this.m_stream, this.m_encoding));
             }
             finally
             {
                 this.m_stream.ReleaseLock();
             }
         }
         this.WriteHeader();
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Sets and <i>opens</i> the file where the log output will go. The specified file must be writable.
 /// </summary>
 /// <param name="fileName">The path to the log file. Must be a fully qualified path.</param>
 /// <param name="append">If true will append to fileName. Otherwise will truncate fileName</param>
 /// <remarks>
 /// <para>
 /// If there was already an opened file, then the previous file
 /// is closed first.
 /// </para>
 /// <para>
 /// This method will ensure that the directory structure
 /// for the <paramref name="fileName" /> specified exists.
 /// </para>
 /// </remarks>
 protected virtual void OpenFile(string fileName, bool append)
 {
     if (LogLog.IsErrorEnabled)
     {
         bool flag = false;
         using (SecurityContext.Impersonate(this))
         {
             flag = Path.IsPathRooted(fileName);
         }
         if (!flag)
         {
             LogLog.Error(declaringType, "INTERNAL ERROR. OpenFile(" + fileName + "): File name is not fully qualified.");
         }
     }
     lock (this)
     {
         Reset();
         LogLog.Debug(declaringType, "Opening file for writing [" + fileName + "] append [" + append.ToString() + "]");
         m_fileName     = fileName;
         m_appendToFile = append;
         LockingModel.CurrentAppender = this;
         LockingModel.OpenFile(fileName, append, m_encoding);
         m_stream = new LockingStream(LockingModel);
         if (m_stream != null)
         {
             m_stream.AcquireLock();
             try
             {
                 SetQWForFiles(m_stream);
             }
             finally
             {
                 m_stream.ReleaseLock();
             }
         }
         WriteHeader();
     }
 }
Exemplo n.º 10
0
        public static void TestSimple()
        {
            using (MemoryStream stream = FakeRuntimeFileInfo.ExpandableMemoryStream(Encoding.UTF8.GetBytes("A short dummy stream")))
            {
                IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath);
                using (LockingStream lockingStream = new LockingStream(fileInfo, stream))
                {
                    Assert.That(FileLock.IsLocked(fileInfo), "The file should be locked now.");
                    Assert.That(lockingStream.CanRead, "The stream should be readable.");
                    Assert.That(lockingStream.CanSeek, "The stream should be seekable.");
                    Assert.That(lockingStream.CanWrite, "The stream should be writeable.");
                    Assert.That(lockingStream.Length, Is.EqualTo("A short dummy stream".Length), "The length should be the same as the string.");

                    byte[] b    = new byte[1];
                    int    read = lockingStream.Read(b, 0, 1);
                    Assert.That(read, Is.EqualTo(1), "There should be one byte read.");
                    Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes("A")[0]), "The byte read should be an 'A'.");
                    Assert.That(lockingStream.Position, Is.EqualTo(1), "After reading the first byte, the position should be at one.");

                    lockingStream.Write(b, 0, 1);
                    lockingStream.Position = 1;
                    read = lockingStream.Read(b, 0, 1);
                    Assert.That(read, Is.EqualTo(1), "There should be one byte read.");
                    Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes("A")[0]), "The byte read should be an 'A'.");

                    lockingStream.Seek(-1, SeekOrigin.End);
                    Assert.That(lockingStream.Position, Is.EqualTo(lockingStream.Length - 1), "The position should be set by the Seek().");

                    lockingStream.SetLength(5);
                    lockingStream.Seek(0, SeekOrigin.End);
                    Assert.That(lockingStream.Position, Is.EqualTo(5), "After setting the length to 5, seeking to the end should set the position at 5.");

                    Assert.DoesNotThrow(() => { lockingStream.Flush(); }, "It's hard to test Flush() behavior here, not worth the trouble, but it should not throw!");
                }
                Assert.That(!FileLock.IsLocked(fileInfo), "The file should be unlocked now.");
                Assert.Throws <ObjectDisposedException>(() => { stream.Position = 0; }, "The underlying stream should be disposed.");
            }
        }
Exemplo n.º 11
0
		/// <summary>
		/// Sets and <i>opens</i> the file where the log output will go. The specified file must be writable.
		/// </summary>
		/// <param name="fileName">The path to the log file. Must be a fully qualified path.</param>
		/// <param name="append">If true will append to fileName. Otherwise will truncate fileName</param>
		/// <remarks>
		/// <para>
		/// If there was already an opened file, then the previous file
		/// is closed first.
		/// </para>
		/// <para>
		/// This method will ensure that the directory structure
		/// for the <paramref name="fileName"/> specified exists.
		/// </para>
		/// </remarks>
		virtual protected void OpenFile(string fileName, bool append)
		{
			if (LogLog.IsErrorEnabled)
			{
				// Internal check that the fileName passed in is a rooted path
				bool isPathRooted = false;
				using(SecurityContext.Impersonate(this))
				{
					isPathRooted = Path.IsPathRooted(fileName);
				}
				if (!isPathRooted)
				{
					LogLog.Error("FileAppender: INTERNAL ERROR. OpenFile("+fileName+"): File name is not fully qualified.");
				}
			}

			lock(this)
			{
				Reset();

				LogLog.Debug("FileAppender: Opening file for writing ["+fileName+"] append ["+append+"]");

				// Save these for later, allowing retries if file open fails
				m_fileName = fileName;
				m_appendToFile = append;

				LockingModel.CurrentAppender=this;
				LockingModel.OpenFile(fileName,append,m_encoding);
				m_stream=new LockingStream(LockingModel);

				if (m_stream != null)
				{
					m_stream.AcquireLock();
					try
					{
						SetQWForFiles(new StreamWriter(m_stream, m_encoding));
					}
					finally
					{
						m_stream.ReleaseLock();
					}
				}

				WriteHeader();
			}
		}