如何管理log4net以登录特定的Url或Web服务 [英] How manage log4net to log into specific Url or Web Service
本文介绍了如何管理log4net以登录特定的Url或Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有Web App,我想将我的日志(使用Log4Net)(错误信息警告)和查询字符串发送到特定URL或发送日志到我之前创建它的Web服务?
I have Web App and I want send my logs (with Log4Net) (Error-Info-Warning) with query string into Specific url OR Send logs to the web service that I created it before?
推荐答案
这是一个类似的项目:
LOG4NET WebServiceAppender [ ^ ]
Here is a project doing something similar:
LOG4NET WebServiceAppender[^]
public class TwitterAppender : AppenderSkeleton
{
#region Private Members
private const string REQUEST_CONTENT_TYPE = "application/x-www-form-urlencoded";
private const string REQUEST_METHOD = "POST";
// The source attribute has been removed from the Twitter API,
// unless you're using OAuth.
// Even if you are using OAuth, there's still an approval process.
// Not worth it; "API" will work for now!
// private const string TWITTER_SOURCE_NAME = "Log4Net";
private const string TWITTER_UPDATE_URL_FORMAT = "http://twitter.com/statuses/update.xml?status={0}";
private string _twitterUserName;
private string _twitterPassword;
/// <summary>
/// This is where we're posting the actual event as a tweet.
/// I was originally thinking of using REST-ful WCF, but, to keep things simple
/// and to reduce dependencies on other technologies, I decided to just use a traditional web request.
/// </summary>
/// <param name="eventToPost_">The event to post.</param>
private void PostLoggingEvent(LoggingEvent eventToPost_)
{
// The base RenderLoggingEvent method converts the event to a string
// based on the layout specified in the configuration file.
// It's important to encode the tweet so that the request URL remains valid.
var updateRequest = HttpWebRequest.Create(String.Format(TWITTER_UPDATE_URL_FORMAT,
HttpUtility.UrlEncode(RenderLoggingEvent(eventToPost_).ToTweet()))) as HttpWebRequest;
updateRequest.ContentLength = 0;
updateRequest.ContentType = REQUEST_CONTENT_TYPE;
updateRequest.Credentials = new NetworkCredential(TwitterUserName, TwitterPassword);
updateRequest.Method = REQUEST_METHOD;
// This is in idiosyncracy with the Twitter API.
updateRequest.ServicePoint.Expect100Continue = true;
var updateResponse = updateRequest.GetResponse() as HttpWebResponse;
if (updateResponse.StatusCode != HttpStatusCode.OK && updateResponse.StatusCode != HttpStatusCode.Continue)
throw new ApplicationException(String.Format("An error occured while invoking the Twitter REST API [Response Code: {0}]; unable to proceed.", updateResponse.StatusCode));
}
#endregion
#region Public Members
/// <summary>
/// This is the password for the Twitter account that is to be used.
/// Fortunately, the Log4Net framework automatically populates
/// this property for us based on the configuration.
/// </summary>
public string TwitterPassword
{
get
{
// We need a password.
if (String.IsNullOrEmpty(_twitterPassword))
throw new ApplicationException("Twitter account password not specified; unable to proceed.");
return _twitterPassword;
}
set
{
_twitterPassword = value;
}
}
/// <summary>
/// This is the user name for the Twitter account that is to be used.
/// Fortunately, the Log4Net framework automatically populates
/// this property for us based on the configuration.
/// </summary>
public string TwitterUserName
{
get
{
// We need a user name.
if (String.IsNullOrEmpty(_twitterUserName))
throw new ApplicationException("Twitter account user name not specified; unable to proceed.");
return _twitterUserName;
}
set
{
_twitterUserName = value;
}
}
#endregion
#region Protected Members
/// <summary>
/// This is the main entry point for the Log4Net framework.
/// </summary>
/// <param name="loggingEvent_">The event to append.</param>
protected override void Append(LoggingEvent loggingEvent_)
{
try
{
if (loggingEvent_ == null)
throw new ArgumentNullException("loggingEvent_");
PostLoggingEvent(loggingEvent_);
}
catch (Exception ex_)
{
// Since we're already inside the event logging framework,
// let Log4Net handle this one.
// We don't want to get stuck in a loop!
ErrorHandler.Error("An error occured while posting the provided event; unable to proceed.", ex_);
}
}
#endregion
}
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net;
使用System.Text;
使用System.Web;
使用log4net。 Appender;
使用log4net.Core;
命名空间TwitterAppender
{
公共类TwitterAppender:AppenderSkeleton
{
#region私人会员
私人const字符串REQUEST_CONTENT_TYPE =application / x-www-form-urlencoded;
私有const字符串REQUEST_METHOD =POST;
//源属性已被删除来自Twitter API,
//除非您使用OAuth。
//即使您使用OAuth,仍然有批准过程。
//不值得; API现在可以使用了!
//私有const字符串TWITTER_SOURCE_NAME =Log4Net;
私有const字符串TWITTER_UPDATE_URL_FORMAT =http ://twitter.com/statuses/update.xml?status = {0};
private string _twitterUserName;
private string _twitterPassword;
///< summary>
///这是我们将实际事件作为推文发布的地方。
///我原本打算使用REST-ful WCF,但是,为了简单起见
///并减少对其他技术的依赖,我决定只需使用传统的网络请求。
///
///< param name =eventToPost_/>要发布的活动。
private void PostLoggingEvent(LoggingEvent eventToPost_)
{
//基础RenderLoggingEvent方法将事件转换为字符串
//基于在配置文件中指定的布局上。
//对推文进行编码以使请求URL保持有效非常重要。
var updateRequest = HttpWebRequest.Create(String.Format(TWITTER_UPDATE_URL_FORMAT,
HttpUtility.UrlEncode(RenderLoggingEvent(eventToPost _)。ToTweet())))作为HttpWebRequest;
updateRequest.ContentLength = 0;
updateRequest.ContentType = REQUEST_CONTENT_TYPE;
updateRequest.Credentials = new NetworkCredential(TwitterUserName,TwitterPassword);
updateRequest.Method = REQUEST_METHOD;
//这是与Twitter API的特殊性。
updateRequest.ServicePoint.Expect100Continue = true;
var updateResponse = updateRequest.GetResponse()as HttpWebResponse;
if(updateResponse.StatusCode!= HttpStatusCode.OK&& updateResponse.StatusCode!= HttpStatusCode.Continue)
抛出新的ApplicationException(String.Format(调用Twitter REST API时发生错误[响应代码:{0}];无法继续。, updateResponse.StatusCode));
}
#endregion
#地区公共会员
///< summary>
///这是要使用的Twitter帐户的密码。
///幸运的是,Log4Net框架会根据配置为我们自动填充
///这个属性。
///
公共字符串TwitterPassword
{
get
{
//我们需要密码。
if(String.IsNullOrEmpty(_twitterPassword))
抛出新的ApplicationException(Twitter未指定帐户密码;无法继续。);
返回_twitterPassword;
}
设置
$
_twitterPassword = value;
}
}
/ //< summary>
///这是要使用的Twitter帐户的用户名。
///幸运的是,Log4Net框架会自动填充
///这个属性为我们根据配置。
///
公共字符串TwitterUserName
{
get
{
//我们需要一个用户名。
if(String.IsNullOrEmpty(_twitterUserName) ))
抛出新的ApplicationException(Twitter帐户用户名未指定;无法继续。);
返回_tw itterUserName;
}
set
{
_twitterUserName = value;
}
}
#endregion
#region受保护的会员>
///< summary>
///这是Log4Net框架的主要入口点。
///
///< param name =loggingEvent_/>要追加的事件。
protected override void Append(LoggingEvent loggingEvent_)
{
尝试
{
if(loggingEvent_ == null)
抛出新的ArgumentNullException( loggingEvent_);
PostLoggingEvent(loggingEvent_);
}
catch(exception ex _)
{
//由于我们已经在事件记录框架内,
//让Log4Net处理这个。
//我们不想陷入困境!
ErrorHandler.Error(发布提供的事件时发生错误;无法继续。, ex_);
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using log4net.Appender;
using log4net.Core;
namespace TwitterAppender
{
public class TwitterAppender : AppenderSkeleton
{
#region Private Members
private const string REQUEST_CONTENT_TYPE = "application/x-www-form-urlencoded";
private const string REQUEST_METHOD = "POST";
// The source attribute has been removed from the Twitter API,
// unless you're using OAuth.
// Even if you are using OAuth, there's still an approval process.
// Not worth it; "API" will work for now!
// private const string TWITTER_SOURCE_NAME = "Log4Net";
private const string TWITTER_UPDATE_URL_FORMAT = "http://twitter.com/statuses/update.xml?status={0}";
private string _twitterUserName;
private string _twitterPassword;
/// <summary>
/// This is where we're posting the actual event as a tweet.
/// I was originally thinking of using REST-ful WCF, but, to keep things simple
/// and to reduce dependencies on other technologies, I decided to just use a traditional web request.
///
/// <param name="eventToPost_" />The event to post.
private void PostLoggingEvent(LoggingEvent eventToPost_)
{
// The base RenderLoggingEvent method converts the event to a string
// based on the layout specified in the configuration file.
// It's important to encode the tweet so that the request URL remains valid.
var updateRequest = HttpWebRequest.Create(String.Format(TWITTER_UPDATE_URL_FORMAT,
HttpUtility.UrlEncode(RenderLoggingEvent(eventToPost_).ToTweet()))) as HttpWebRequest;
updateRequest.ContentLength = 0;
updateRequest.ContentType = REQUEST_CONTENT_TYPE;
updateRequest.Credentials = new NetworkCredential(TwitterUserName, TwitterPassword);
updateRequest.Method = REQUEST_METHOD;
// This is in idiosyncracy with the Twitter API.
updateRequest.ServicePoint.Expect100Continue = true;
var updateResponse = updateRequest.GetResponse() as HttpWebResponse;
if (updateResponse.StatusCode != HttpStatusCode.OK && updateResponse.StatusCode != HttpStatusCode.Continue)
throw new ApplicationException(String.Format("An error occured while invoking the Twitter REST API [Response Code: {0}]; unable to proceed.", updateResponse.StatusCode));
}
#endregion
#region Public Members
/// <summary>
/// This is the password for the Twitter account that is to be used.
/// Fortunately, the Log4Net framework automatically populates
/// this property for us based on the configuration.
///
public string TwitterPassword
{
get
{
// We need a password.
if (String.IsNullOrEmpty(_twitterPassword))
throw new ApplicationException("Twitter account password not specified; unable to proceed.");
return _twitterPassword;
}
set
{
_twitterPassword = value;
}
}
/// <summary>
/// This is the user name for the Twitter account that is to be used.
/// Fortunately, the Log4Net framework automatically populates
/// this property for us based on the configuration.
///
public string TwitterUserName
{
get
{
// We need a user name.
if (String.IsNullOrEmpty(_twitterUserName))
throw new ApplicationException("Twitter account user name not specified; unable to proceed.");
return _twitterUserName;
}
set
{
_twitterUserName = value;
}
}
#endregion
#region Protected Members
/// <summary>
/// This is the main entry point for the Log4Net framework.
///
/// <param name="loggingEvent_" />The event to append.
protected override void Append(LoggingEvent loggingEvent_)
{
try
{
if (loggingEvent_ == null)
throw new ArgumentNullException("loggingEvent_");
PostLoggingEvent(loggingEvent_);
}
catch (Exception ex_)
{
// Since we're already inside the event logging framework,
// let Log4Net handle this one.
// We don't want to get stuck in a loop!
ErrorHandler.Error("An error occured while posting the provided event; unable to proceed.", ex_);
}
}
#endregion
}
}
这篇关于如何管理log4net以登录特定的Url或Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文