Example #1
0
 protected override void WriteHeader()
 {
     if (LockingModel.AcquireLock().Length == 0)
     {
         base.WriteHeader();
     }
 }
Example #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ele">配置元素</param>
        public FileAppenderConfig(XElement ele)
            : base(ele)
        {
            //base.EnableOutputCache = true;
            this.MaxFileSize = 10 * 1024;//默认值10MB

            if (ele == null)
            {
                return;
            }

            int days;

            if (int.TryParse(LogUtil.GetChildXElementValue(ele, nameof(this.Days)), out days))
            {
                this.Days = days;
            }

            int maxFileCount;

            if (int.TryParse(LogUtil.GetChildXElementValue(ele, nameof(this.MaxFileCount)), out maxFileCount))
            {
                if (maxFileCount < 1 && maxFileCount != -1)
                {
                    maxFileCount = -1;
                }

                this.MaxFileCount = maxFileCount;
            }

            int maxFileSize;

            if (int.TryParse(LogUtil.GetChildXElementValue(ele, nameof(this.MaxFileSize)), out maxFileSize))
            {
                this.MaxFileSize = maxFileSize;
            }

            string filePath = LogUtil.GetChildXElementValue(ele, nameof(this.FilePath)).Trim();

            if (!string.IsNullOrWhiteSpace(filePath))
            {
                this.FilePath = filePath.Trim();
            }

            bool isAppend;

            if (bool.TryParse(LogUtil.GetChildXElementValue(ele, nameof(this.IsAppend)).Trim(), out isAppend))
            {
                this.IsAppend = isAppend;
            }

            this.SecurityPolicy = LogUtil.GetChildXElementValue(ele, nameof(this.SecurityPolicy)).Trim();

            LockingModel lockingType;

            if (Enum.TryParse <LockingModel>(LogUtil.GetChildXElementValue(ele, nameof(this.LockingModel)).Trim(), out lockingType))
            {
                this.LockingModel = lockingType;
            }
        }
Example #3
0
        protected override void WriteHeader()
        {
            if (LockingModel.AcquireLock().Length == 0)
            {
                try
                {
                    var log4netSection = ConfigurationManager.GetSection("log4net") as XmlElement;
                    var innerXML       = log4netSection.InnerXml;
                    innerXML = "<RandomTag>" + innerXML + "</RandomTag>";

                    var log4netData = new XmlDocument();
                    log4netData.LoadXml(innerXML);
                    var footer             = log4netData.GetElementsByTagName("footer").Cast <XmlElement>().First();
                    var fileElement        = log4netData.GetElementsByTagName("file").Cast <XmlElement>().First();
                    var fileValue          = fileElement.GetAttribute("value");
                    var datePatternElement = log4netData.GetElementsByTagName("datePattern").Cast <XmlElement>().First();
                    var datePatternValue   = datePatternElement.GetAttribute("value");
                    var extension          = datePatternValue.Substring(datePatternValue.IndexOf("\'") + 1);
                    var format             = datePatternValue.Substring(0, datePatternValue.Substring(0, datePatternValue.IndexOf("\'")).Length);
                    if (fileValue.Contains("LogDirectory"))
                    {
                        var fileDir     = fileValue.Substring(0, fileValue.IndexOf('%'));
                        var directories = Directory.GetDirectories(fileDir);
                        foreach (var dir in directories)
                        {
                            var folderName = dir.Remove(0, dir.Substring(0, dir.LastIndexOf('\\')).Length + 1);
                            var filename   = fileValue.Replace("%property{LogDirectory}", folderName) + DateTime.Now.AddDays(-1).ToString(format);

                            var loc = filename + extension.Substring(0, extension.Length - 1);

                            string file = System.IO.File.ReadAllText(loc);

                            if (!file.Contains(footer.GetAttribute("value")))
                            {
                                System.IO.File.WriteAllText(loc, file + "\n" + footer.GetAttribute("value"), Encoding.UTF8);
                            }
                        }
                    }
                    else
                    {
                        var filename = fileValue + DateTime.Now.AddDays(-1).ToString(format);

                        var loc = filename + extension.Substring(0, extension.Length - 1);

                        string file = System.IO.File.ReadAllText(loc);

                        if (!file.Contains(footer.GetAttribute("value")))
                        {
                            System.IO.File.WriteAllText(loc, file + "\n" + footer.GetAttribute("value"), Encoding.UTF8);
                        }
                    }
                }
                catch (Exception ex) { }

                base.WriteHeader();
            }
        }
    protected override void Append(LoggingEvent loggingEvent)
    {
        CloseFile();
        File = fileNamePattern.Replace("__filename__", ThreadContext.Properties["PropertyName"].ToString());
        LockingModel.OpenFile(File, true, Encoding.UTF8);
        LockingModel.AcquireLock();
        OpenFile(File, true);

        base.Append(loggingEvent);
        DoAppend(loggingEvent);
    }
Example #5
0
 protected override void WriteHeader()
 {
     try
     {
         if (LockingModel.AcquireLock().Length == 0)
         {
             base.WriteHeader();
         }
     }
     finally
     {
         LockingModel.ReleaseLock();
     }
 }
Example #6
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)
            {
                // Internal check that the fileName passed in is a rooted path
                var 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();
            }
        }
Example #7
0
 public void SetUp()
 {
     mSut = new LockingModel();
 }
Example #8
0
 public void SetUp()
 {
     mSut = new LockingModel(new ReadOnlyCollection <IProperty>(new List <IProperty>()));
 }