2013-03-07 41 views
4

當日志消息包含嵌入的新行字符時,日誌文件中這些日誌消息的對齊方式不正確。使用log4net在日誌消息中處理嵌入換行符

例如,如果我使用的變換圖案: [%-5level]%消息%的換行符

,如果我記錄異常堆棧跟蹤它包含嵌入的新行字符,或任何其它多行日誌消息,則消息中的附加行從行的開始處開始。

對於每個這樣的附加行,是否可能遵循轉換模式,並且文本是否適當縮進?

回答

1

我做了以下方法:

void Log(string message, int levelsDeep) 
{ 
    StringBuilder sb = new StringBuilder(); 
    for(int i = 0; i < levelsDeep; i++) 
     sb.Append(" "); 

    string spacer = sb.ToString(); 

    string msg = message.Replace("\r\n", "\r\n" + spacer); 
    msg = "\r\n" + msg + "\r\n"; //the prefix and suffix newline characters ensure that this and the next message starts in a new line and is not impacted by the 'spacer' from this message. 

    // call log4net functions to log the 'msg' 

}