2012-04-03 86 views

回答

1

的le_nlog包已經在最近幾天被更新與抓住appharbor相應的代碼注入配置變量從web.config,所以現在可以安裝nuget,將附加組件添加到您的應用程序中,並且無需手動編輯任何內容即可輕鬆完成。除非你想從本地機器上登錄,否則在上面的例子中就已經指出了,在這種情況下,配置變量應該被粘貼到web.config.transform中的appSettings部分的web.config中le_nlog包

2

您不必手動添加配置,AppHarbor會自動插入相關的值。請注意,如果您想在本地計算機上測試時使用LogEntries,則需要將配置指定爲從AppHarbor複製的配置。

+0

我發現,因爲自定義nlog目標是在AppHarbor變換無法插入自定義密鑰和位置值(或者他們可以,但它們在AppSettings中)的自定義配置部分中配置的。我使用了類似於下面的解決方案,我修改它以讀取常規AppSetting條目而不是定製配置節。 – 2012-04-05 06:11:00

0

使用this class而不是le_nlog包中的那個。同時更改組件的配置:

<nlog> 
<extensions> 
    <add assembly="MyAssembly"/> 
</extensions> 
<targets> 
    <target name="logentries" type="Logentries" debug="true" layout="${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}, ${exception:format=tostring}" /> 
</targets> 
<rules> 
    <logger name="*" minLevel="Info" appendTo="logentries" /> 
</rules> 

/* 
    Logentries Log4Net Logging agent 
    Copyright 2010,2011 Logentries, Jlizard 
    Mark Lacomber <[email protected]> 
              */ 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Net.Security; 
using System.Net.Sockets; 
using System.IO; 

using NLog; 
using NLog.Common; 
using NLog.Config; 
using NLog.Internal; 
using NLog.Internal.NetworkSenders; 
using NLog.Layouts; 
using NLog.Targets; 

namespace Le 
{ 
    [Target("Logentries")] 
    public sealed class LeTarget : TargetWithLayout 
    { 
     private SslStream sslSock = null; 
     private TcpClient leSocket = null; 
     private System.Text.UTF8Encoding encoding; 

     public LeTarget() 
     { 

     } 

     string GetKey() 
     { 
      return ConfigurationManager.AppSettings["LOGENTRIES_ACCOUNT_KEY"]; 
     } 

     string GetLocation() 
     { 
      return ConfigurationManager.AppSettings["LOGENTRIES_LOCATION"]; 
     } 

     [RequiredParameter] 
     public bool Debug { get; set; } 

     public bool KeepConnection { get; set; } 

     private void createSocket(String key, String location) 
     { 
      this.leSocket = new TcpClient("api.logentries.com", 443); 
      this.leSocket.NoDelay = true; 
      this.sslSock = new SslStream(this.leSocket.GetStream()); 
      this.encoding = new System.Text.UTF8Encoding(); 

      this.sslSock.AuthenticateAsClient("logentries.com"); 

      String output = "PUT /" + key + "/hosts/" + location + "/?realtime=1 HTTP/1.1\r\n"; 
      this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length); 
      output = "Host: api.logentries.com\r\n"; 
      this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length); 
      output = "Accept-Encoding: identity\r\n"; 
      this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length); 
      output = "Transfer_Encoding: chunked\r\n\r\n"; 
      this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length); 
     } 

     private byte[] GetBytesToWrite(LogEventInfo logEvent) 
     { 
      string text = this.Layout.Render(logEvent) + "\r\n"; 

      return this.encoding.GetBytes(text); 
     } 

     protected override void Write(LogEventInfo logEvent) 
     { 
      if (this.sslSock == null) 
      { 
       try 
       { 
        this.createSocket(this.GetKey(), this.GetLocation()); 
       } 
       catch (Exception e) 
       { 
        WriteDebugMessages("Error connecting to Logentries", e); 
       } 
      } 

      byte[] message = this.GetBytesToWrite(logEvent); 

      try 
      { 
       this.sendToLogentries(message); 
      } 
      catch (Exception) 
      { 
       try 
       { 
        this.createSocket(this.GetKey(), this.GetLocation()); 
        this.sendToLogentries(message); 
       } 
       catch (Exception ex) 
       { 
        WriteDebugMessages("Error sending log to Logentries", ex); 
       } 
      } 
     } 

     private void sendToLogentries(byte[] message) 
     { 
      this.sslSock.Write(message, 0, message.Length); 
     } 

     private void WriteDebugMessages(string message, Exception e) 
     { 
      if (!this.Debug) return; 
      string[] messages = { message, e.ToString() }; 
      foreach (var msg in messages) 
      { 
       System.Diagnostics.Debug.WriteLine(msg); 
       Console.Error.WriteLine(msg); 
      } 
     } 
    } 
}