/// <summary> /// This method is called by the <see cref="M:AppenderSkeleton.DoAppend(LoggingEvent)" /> /// method. /// </summary> /// <param name="loggingEvent">The event to log.</param> /// <remarks> /// <para> /// Writes a log statement to the output stream if the output stream exists /// and is writable. /// </para> /// <para> /// The format of the output will depend on the appender's layout. /// </para> /// </remarks> protected override void Append(LoggingEvent loggingEvent) { if (m_stream.AcquireLock()) { try { base.Append(loggingEvent); } finally { m_stream.ReleaseLock(); } } }
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(); } } } }
/// <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(); } }
/// <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(); } } } }
/// <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(); } } }
/// <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(); } }
/// <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(); } } } }
/// <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(); } } } }
/// <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(); } }
/// <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(); } }