加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用log4net处理日志消息中的嵌入式换行符

发布时间:2020-12-16 07:03:43 所属栏目:百科 来源:网络整理
导读:当日志消息包含嵌入的新行字符时,日志文件中的此类日志消息的对齐方式不正确. 例如,如果我使用转换模式: ????[%-5level]%message%newline 如果我记录包含嵌入的新行字符或任何其他多行日志消息的异常堆栈跟踪,则消息中的其他行从该行的开头开始. 是否有
当日志消息包含嵌入的新行字符时,日志文件中的此类日志消息的对齐方式不正确.

例如,如果我使用转换模式:
????[%-5level]%message%newline

如果我记录包含嵌入的新行字符或任何其他多行日志消息的异常堆栈跟踪,则消息中的其他行从该行的开头开始.

是否有可能对于每个这样的附加行,遵循转换模式,文本是否缩进?

解决方法

我这样做的方式如下:

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("rn","rn" + spacer);
   msg = "rn" + msg + "rn";   //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'

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读