首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从环境中读取LogEntries配置

从环境中读取LogEntries配置
EN

Stack Overflow用户
提问于 2012-04-04 02:58:00
回答 3查看 535关注 0票数 2

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-09 05:43:14

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

票数 1
EN

Stack Overflow用户

发布于 2012-04-04 06:49:09

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

票数 2
EN

Stack Overflow用户

发布于 2012-04-04 11:49:57

使用this class而不是le_nlog包中的那个。还要在配置中更改您的程序集:

代码语言:javascript
复制
<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>

代码语言:javascript
复制
/*
   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);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9999607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档