log4net syslog appender对新行有一些问题

时间:2016-06-19 18:02:42

标签: log4net syslog

我有这个appender:

 <appender name="Syslog" type="log4net.Appender.RemoteSyslogAppender">
    <param name="RemoteAddress" value="**.***.***.**" />
    <param name="RemotePort" value="514" />

    <facility value="Local6" />
    <identity value="Widgets" />

    <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="[%date] [%property{NDC}] [%-5level] [%logger] [%property{url}] [%message] [%thread] %newline" />
      </layout>
  </appender>

appender将 stacktrace 写为 new 日志条目:

Jun 19 20:06:33 adminclou6PBAYR Widgets: [2016-06-19 20:06:21,156] [0JUK68M] [ERROR] [Widgets.Web.HandleWebErrorAttribute] [~/Customer/RegisterWithEmailForm] [The given key was not present in the dictionary.] [16]
Jun 19 20:06:33 adminclou6PBAYR Widgets: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at Widgets.Web.LabelExtensions.TranslatedLabelFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression, IDictionary`2 htmlAttributes)
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at ASP._Page_Views_Customer_RegisterWithEmailForm_cshtml.Execute() in c:\inetpub\wwwroot\HL\Widgets\Views\Customer\RegisterWithEmailForm.cshtml:line 4
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
Jun 19 20:06:33 adminclou6PBAYR Widgets:    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)

RollingFileAppender BTW很好。

  1. 如何防止堆栈跟踪一次又一次地写入新的日志条目?
  2. RollingFileAppenderRemoteSyslogAppender中,我没有要求编写堆栈跟踪,就像你在conversionPattern中看到的那样,但是appender仍然会写它。
  3. 由于

1 个答案:

答案 0 :(得分:1)

您的邮件包含换行符,这些换行符在remotesysappender code (line 396):

中作为单独的行发送
373                 while (i < message.Length) 
374                 { 
375                     // Clear StringBuilder 
376                     builder.Length = 0; 
377  
378                     // Write priority 
379                     builder.Append('<'); 
380                     builder.Append(priority); 
381                     builder.Append('>'); 
382  
383                     // Write identity 
384                     builder.Append(identity); 
385                     builder.Append(": "); 
386  
387                     for (; i < message.Length; i++) 
388                     { 
389                         c = message[i]; 
390  
391                         // Accept only visible ASCII characters and space. See RFC 3164 section 4.1.3 
392                         if (((int)c >= 32) && ((int)c <= 126)) 
393                         { 
394                             builder.Append(c); 
395                         } 
396                         // If character is newline, break and send the current line 
397                         else if ((c == '\r') || (c == '\n')) 
398                         { 
399                             // Check the next character to handle \r\n or \n\r 
400                             if ((message.Length > i + 1) && ((message[i + 1] == '\r') || (message[i + 1] == '\n'))) 
401                             { 
402                                 i++; 
403                             } 
404                             i++; 
405                             break; 
406                         } 
407                     } 
408  
409                     // Grab as a byte array 
410                     buffer = this.Encoding.GetBytes(builder.ToString()); 
411  
412                     this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint); 
413                 } 

您可以通过不在消息(转换器)中添加\ r或\ n来防止这种情况。