【问题标题】:Reading LogEntries configuration from the environment从环境中读取 LogEntries 配置
【发布时间】:2012-04-09 05:52:02
【问题描述】:

根据LogEntries documentation 帐户密钥应填写在Web.config 文件中。同时存在于 AppHarbor 配置变量中。 我可以从配置变量中读取值而不是使用硬编码值吗?

【问题讨论】:

    标签: appharbor logentries


    【解决方案1】:

    您不必手动添加配置,AppHarbor 会自动插入相关值。请注意,如果您想在本地机器上测试时使用 LogEntries,则需要指定从 AppHarbor 复制的配置。

    【讨论】:

    • 我发现因为自定义 nlog 目标是在自定义配置部分中配置的,所以 AppHarbor 转换无法插入自定义 Key 和 Location 值(或者它们可能会插入,但它们在 AppSettings 中)。我使用了类似于下面的解决方案,我将其修改为读取常规 AppSetting 条目而不是自定义配置部分。
    【解决方案2】:

    le_nlog 包已在过去几天更新了适当的代码,用于从 web.config 中获取 appharbor 注入的配置变量,因此现在可以安装 nuget,将附加组件添加到您的应用程序中无需手动编辑任何内容即可。当然,除非如上所述,在您想从本地计算机登录的情况下,在这种情况下,配置变量应该粘贴到 appSettings 部分的 web.config 中,该部分现在包含在 web.config.transform 中le_nlog 包

    【讨论】:

      【解决方案3】:

      使用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 <marklacomber@gmail.com>
                                                  */
      
      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);
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-30
        • 2020-03-06
        • 2018-09-06
        • 1970-01-01
        • 2017-03-06
        • 2019-08-27
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多