2016-12-12 81 views
4

我們實現了自定義日誌並寫入自定義日誌文件。 的問題是,該日誌被創建,每天數次用文件名是這樣的:Sitecore自定義日誌,如何每天只寫一個文件?

MyCustom.log.20161208.165109 
MyCustom.log.20161208.165845 
MyCustom.log.20161208.175134 
MyCustom.log.20161208.184432 

的定義是這樣的:

<appender name="MyCustomLogAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging"> 
    <file value="$(dataFolder)/logs/MyCustom.log.{date}.txt" /> 
    <appendToFile value="true" /> 
    <layout type="log4net.Layout.PatternLayout"> 
    <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" /> 
    </layout> 
    <encoding value="utf-8" /> 
</appender> 

誰能告訴我什麼,我需要配置,在爲了每天只接收一個文件?換句話說,WHEN是否創建了一個新文件?

+2

這是否發生在您當地的環境或產品上?您的應用程序池是否已回收只要您的應用程序未重新啓動,如果您不使用滾動文件appender,則應該只有1個日誌文件。 –

+0

謝謝Marek提示,我不知道每次應用程序池被回收時都會創建一個新日誌。 – Bgl86

回答

3

Sitecore的使用log4net的,因爲它的底層日誌框架,這意味着你可以使用任何標準的追加程序的。儘管Sitecore已經添加了自定義的appender,但您可以簡單地使用RollingFileAppender並根據日期滾動日誌文件。

您可以在文檔的log4net config examples section中找到樣品。具體而言與Sitecore的

,改變附加器(一個或多個),以執行以下操作:

  • file

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, Sitecore.Logging"> 
        <file value="$(dataFolder)/logs/log" /> 
        <appendToFile value="true" /> 
        <rollingStyle value="Date" /> 
        <maxSizeRollBackups value="30" /> 
        <datePattern value=".yyyyMMdd'.txt'" /> 
        <staticLogFileName value="false" /> 
        <layout type="log4net.Layout.PatternLayout"> 
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" /> 
        </layout> 
        <encoding value="utf-8" /> 
    </appender> 
    

    關於在上述結構中的變化的一些細節的文件的名稱登錄至。請注意,我們使用以下設置對此進行補充

  • rollingStyle:打開每個文件的格式。
  • maxSizeRollBackups:這已設置爲30以上,如果需要,可以刪除此節點。該值確保log4net自動刪除超過30天的anyt日誌。
  • datePattern:這設置日期的格式以滾動文件。請注意,文件後綴包含在單引號中。有關更多詳細信息,請參閱此previous answer
  • :如果設置爲true,則最新的日誌文件將始終具有相同的名稱,但請注意,由於您的file值沒有文件後綴。

的文件就會在同一個日誌文件夾格式log.yyyMMdd.txt如之前產生,而不是受到具有每重新啓動應用程序/應用程序池再循環中產生的不同的文件。

1

如果您重寫SitecoreLogFileAppender的標準行爲並創建一個從FileAppender繼承的新CustomLogFileAppender類,則可以輕鬆解決此問題。不幸的是,SitecoreLogFileAppender不提供您僅覆蓋您需要根據您的要求修改的特定方法的可能性。因此,您需要將SitecoreLogFileAppender中的所有代碼複製到新創建的文件appender中。

namespace MyProject.Core 
 
{ 
 
    using System; 
 
    using System.Collections.Generic; 
 
    using System.IO; 
 
    using System.Runtime.InteropServices; 
 
    using System.Web; 
 
    using log4net.Appender; 
 
    using log4net.spi; 
 

 
    /// <summary> 
 
    /// Custom log file appender 
 
    /// </summary> \t 
 
    public class CustomLogFileAppender : FileAppender 
 
    { 
 
     /// <summary> 
 
     /// The m_current date 
 
     /// </summary> 
 
     private DateTime m_currentDate; 
 

 
     /// <summary> 
 
     /// The m_original file name 
 
     /// </summary> 
 
     private string m_originalFileName; 
 

 
     /// <summary> 
 
     /// Initializes a new instance of the <see cref="CustomLogFileAppender"/> class. 
 
     /// </summary> 
 
     public CustomLogFileAppender() 
 
     { 
 
      this.m_currentDate = DateTime.Now; 
 
     } 
 

 
     /// <summary> 
 
     /// Gets or sets the file. 
 
     /// </summary> 
 
     /// <value> 
 
     /// The file. 
 
     /// </value> 
 
     public override string File 
 
     { 
 
      get 
 
      { 
 
       return base.File; 
 
      } 
 

 
      set 
 
      { 
 
       if (this.m_originalFileName == null) 
 
       { 
 
        string str = value; 
 
        Dictionary<string, string> variables = ConfigReader.GetVariables(); 
 
        foreach (string index in variables.Keys) 
 
        { 
 
         string oldValue = "$(" + index + ")"; 
 
         str = str.Replace(oldValue, variables[index]); 
 
        } 
 

 
        this.m_originalFileName = this.MapPath(str.Trim()); 
 
       } 
 

 
       base.File = this.m_originalFileName; 
 
      } 
 
     } 
 

 
     /// <summary> 
 
     /// Makes the path. 
 
     /// </summary> 
 
     /// <param name="part1">The part1.</param> 
 
     /// <param name="part2">The part2.</param> 
 
     /// <param name="separator">The separator.</param> 
 
     /// <returns> 
 
     /// Complete path. 
 
     /// </returns> 
 
     public static string MakePath(string part1, string part2, char separator) 
 
     { 
 
      if (string.IsNullOrEmpty(part1)) 
 
      { 
 
       return part2 ?? string.Empty; 
 
      } 
 

 
      if (string.IsNullOrEmpty(part2)) 
 
      { 
 
       return part1 ?? string.Empty; 
 
      } 
 

 
      if ((int)part1[part1.Length - 1] == (int)separator) 
 
      { 
 
       part1 = part1.Substring(0, part1.Length - 1); 
 
      } 
 

 
      if ((int)part2[0] == (int)separator) 
 
      { 
 
       part2 = part2.Substring(1); 
 
      } 
 

 
      return part1 + (object)separator + part2; 
 
     } 
 

 
     /// <summary> 
 
     /// Appends the specified logging event. 
 
     /// </summary> 
 
     /// <param name="loggingEvent">The logging event.</param> 
 
     protected override void Append(LoggingEvent loggingEvent) 
 
     { 
 
      DateTime now = DateTime.Now; 
 
      if (this.m_currentDate.Day != now.Day || this.m_currentDate.Month != now.Month || this.m_currentDate.Year != now.Year) 
 
      { 
 
       lock (this) 
 
       { 
 
        this.CloseFile(); 
 
        this.m_currentDate = DateTime.Now; 
 
        this.OpenFile(string.Empty, false); 
 
       } 
 
      } 
 

 
      base.Append(loggingEvent); 
 
     } 
 

 
     /// <summary> 
 
     /// Opens the file. 
 
     /// </summary> 
 
     /// <param name="fileName">Name of the file.</param> 
 
     /// <param name="append">if set to <c>true</c> [append].</param> 
 
     protected override void OpenFile(string fileName, bool append) 
 
     { 
 
      fileName = this.m_originalFileName; 
 
      fileName = fileName.Replace("{date}", this.m_currentDate.ToString("yyyyMMdd")); 
 
      fileName = fileName.Replace("{time}", this.m_currentDate.ToString("HHmmss")); 
 
      fileName = fileName.Replace("{processid}", CustomLogFileAppender.GetCurrentProcessId().ToString()); 
 

 
      base.OpenFile(fileName, append); 
 
     } 
 

 
     /// <summary> 
 
     /// Gets the current process identifier. 
 
     /// </summary> 
 
     /// <returns> 
 
     /// The process Id. 
 
     /// </returns> 
 
     [DllImport("kernel32.dll", SetLastError = true)] 
 
     private static extern int GetCurrentProcessId(); 
 

 
     /// <summary> 
 
     /// Gets the name of the timed file. 
 
     /// </summary> 
 
     /// <param name="fileName">Name of the file.</param> 
 
     /// <returns> 
 
     /// Filename with timestamp. 
 
     /// </returns> 
 
     private string GetTimedFileName(string fileName) 
 
     { 
 
      int num = fileName.LastIndexOf('.'); 
 
      if (num < 0) 
 
      { 
 
       return fileName; 
 
      } 
 

 
      return 
 
       string.Concat(
 
        new object[4] 
 
         { 
 
          (object)fileName.Substring(0, num), (object)'.', (object)this.m_currentDate.ToString("HHmmss"), 
 
          (object)fileName.Substring(num) 
 
         }); 
 
     } 
 

 
     /// <summary> 
 
     /// Determines whether the specified file name is locked. 
 
     /// </summary> 
 
     /// <param name="fileName">Name of the file.</param> 
 
     /// <returns> 
 
     /// Locked or not. 
 
     /// </returns> 
 
     private bool IsLocked(string fileName) 
 
     { 
 
      if (!System.IO.File.Exists(fileName)) 
 
      { 
 
       return false; 
 
      } 
 

 
      try 
 
      { 
 
       FileStream fileStream = System.IO.File.OpenWrite(fileName); 
 
       if (fileStream == null) 
 
       { 
 
        return true; 
 
       } 
 

 
       fileStream.Close(); 
 
       return false; 
 
      } 
 
      catch (Exception ex) 
 
      { 
 
       string message = ex.Message; 
 
       return true; 
 
      } 
 
     } 
 

 
     /// <summary> 
 
     /// Maps the path. 
 
     /// </summary> 
 
     /// <param name="fileName">Name of the file.</param> 
 
     /// <returns> 
 
     /// Mapped path. 
 
     /// </returns> 
 
     private string MapPath(string fileName) 
 
     { 
 
      if (fileName == string.Empty || fileName.IndexOf(":/", System.StringComparison.Ordinal) >= 0 || fileName.IndexOf("://", System.StringComparison.Ordinal) >= 0) 
 
      { 
 
       return fileName; 
 
      } 
 

 
      var index = fileName.IndexOfAny(new char[2] { '\\', '/' }); 
 
      if (index >= 0 && (int)fileName[index] == 92) 
 
      { 
 
       return fileName.Replace('/', '\\'); 
 
      } 
 

 
      fileName = fileName.Replace('\\', '/'); 
 
      if (HttpContext.Current != null) 
 
      { 
 
       return HttpContext.Current.Server.MapPath(fileName); 
 
      } 
 

 
      return (int)fileName[0] == 47 ? SitecoreLogFileAppender.MakePath(HttpRuntime.AppDomainAppPath, fileName.Replace('/', '\\'), '\\') : fileName; 
 
     } 
 
    } 
 
}

請在這裏找到更多的信息:https://sitecore.unic.com/2015/01/27/create-a-single-sitecore-log-file-per-day

相關問題