- log4netwebserviceappender_demo.zip
- Log4NetWebServiceAppender.sln
- Log4NetWebServiceAppender.suo
- References
- ClickOnceButton.dll
- de
- ZetaLib.Core.resources.dll
- ZetaLib.Web.resources.dll
- ZetaLib.Windows.resources.dll
- ICSharpCode.SharpZipLib.dll
- Interop.ADODB.dll
- Interop.JRO.dll
- log4net.dll
- ZetaLib.Core.dll
- ZetaLib.Web.dll
- ZetaLib.Windows.dll
- Runtime
- TestClient
- Web
- App_Code
- Bin
- ClickOnceButton.dll
- de
- ZetaLib.Core.resources.dll
- ZetaLib.Web.resources.dll
- ICSharpCode.SharpZipLib.dll
- Interop.ADODB.dll
- Interop.JRO.dll
- log4net.dll
- ZetaLib.Core.dll
- ZetaLib.Core.dll.refresh
- ZetaLib.Web.dll
- ZetaLib.Web.dll.refresh
- Global.asax
- LoggingService.asmx
- Web.Config
|
namespace Zeta
{
#region Using directives.
// ----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Core;
using log4net.Appender;
using log4net.Util;
using ZetaLib.Core.Common;
using System.ComponentModel;
using log4net.Layout;
using System.Net;
using log4net.Filter;
using System.Diagnostics;
using ZetaLib.Core.Logging;
// ----------------------------------------------------------------------
#endregion
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// An appender for LOG4NET that is capable of appending to a web service.
/// </summary>
/// <remarks>
/// See http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/tpage/1/view/topic/postid/17080/Default.aspx#17919
///
/// Available configuration options:
///
/// <appender name="WebServiceAppender" type="Zeta.WebServiceAppender">
/// <!-- LOG4NET parameters. -->
/// <filter type="log4net.Filter.LevelRangeFilter">
/// <param name="LevelMin" value="DEBUG"/>
/// <param name="LevelMax" value="FATAL"/>
/// </filter>
///
/// <!-- General parameters. -->
/// <param name="ApiKey" value="{0E12B21A-95B9-11DB-96C0-005056C00008}" />
///
/// <!-- WebService parameters. -->
/// <param name="Url" value="http://www.myserver.com/LoggingService.asmx" />
/// <param name="TimeoutSeconds" value="60" />
///
/// <!-- Proxy parameters. -->
/// <param name="UseProxy" value="false" />
/// <param name="ProxyUrl" value="http://myproxy:3128" />
/// <param name="ProxyBypassOnLocal" value="true" />
/// <param name="ProxyUseDefaultCredentials" value="true" />
/// <param name="ProxyCredentialsDomain" value="OFFICE" />
/// <param name="ProxyCredentialsUserName" value="MyUser" />
/// <param name="ProxyCredentialsPassword" value="MyPassword" />
/// </appender>
/// </remarks>
public class WebServiceAppender :
AppenderSkeleton
{
#region Public properties, setable from the configuration file.
// ------------------------------------------------------------------
/// <summary>
///
/// </summary>
public LevelRangeFilter Filter
{
get
{
return filter;
}
set
{
filter = value;
}
}
/// <summary>
///
/// </summary>
public string ApiKey
{
get
{
return apiKey;
}
set
{
apiKey = value;
}
}
/// <summary>
///
/// </summary>
public string Url
{
get
{
return url;
}
set
{
url = value;
}
}
/// <summary>
///
/// </summary>
public int TimeoutSeconds
{
get
{
return timeoutSeconds;
}
set
{
timeoutSeconds = value;
}
}
/// <summary>
///
/// </summary>
public bool UseProxy
{
get
{
return useProxy;
}
set
{
useProxy = value;
}
}
/// <summary>
///
/// </summary>
public string ProxyUrl
{
get
{
return proxyUrl;
}
set
{
proxyUrl = value;
}
}
/// <summary>
///
/// </summary>
public bool ProxyBypassOnLocal
{
get
{
return proxyBypassOnLocal.GetValueOrDefault( true );
}
set
{
proxyBypassOnLocal = value;
}
}
/// <summary>
///
/// </summary>
public bool ProxyUseDefaultCredentials
{
get
{
return proxyUseDefaultCredentials.GetValueOrDefault( true );
}
set
{
proxyUseDefaultCredentials = value;
}
}
/// <summary>
///
/// </summary>
public string ProxyCredentialsDomain
{
get
{
return proxyCredentialsDomain;
}
set
{
proxyCredentialsDomain = value;
}
}
/// <summary>
///
/// </summary>
public string ProxyCredentialsUserName
{
get
{
return proxyCredentialsUserName;
}
set
{
proxyCredentialsUserName = value;
}
}
/// <summary>
///
/// </summary>
public string ProxyCredentialsPassword
{
get
{
return proxyCredentialsPassword;
}
set
{
proxyCredentialsPassword = value;
}
}
// ------------------------------------------------------------------
#endregion
#region Implementation of IOptionHandler.
// ------------------------------------------------------------------
/// <summary>
///
/// </summary>
public override void ActivateOptions()
{
base.ActivateOptions();
//Provide any checks based on Service config properties, if Service
//config properties are valid initialize WebServiceAppender.
InitializeServiceConnection();
}
// ------------------------------------------------------------------
#endregion
#region Override implementation of AppenderSkeleton.
// ------------------------------------------------------------------
/// <summary>
///
/// </summary>
protected override void Append(
LoggingEvent loggingEvent )
{
try
{
// Call the web service, but only if filtered correctly.
if ( filter == null ||
(loggingEvent.Level >= filter.LevelMin &&
loggingEvent.Level <= filter.LevelMax) )
{
string message = loggingEvent.RenderedMessage;
if ( !string.IsNullOrEmpty( message ) )
{
message = message.Trim();
}
Logging.WebServiceCommunicationInformation info =
new Zeta.Logging.WebServiceCommunicationInformation();
info.ApiKey = apiKey;
info.ExceptionSerializedAsString =
StringHelper.SerializeToString(
loggingEvent.ExceptionObject );
info.Level = loggingEvent.Level.Name;
info.Message = message;
// Provide additional context information.
info.ContextInformation =
ZetaLib.Core.Logging.LoggingInformation.All;
// Call the web service.
_serviceLogging.LogAsync(
info,
Guid.NewGuid() );
}
}
catch ( Exception exc )
{
ErrorHandler.Error(
"Unable to send loggingEvent to Logging Service",
exc,
ErrorCode.WriteFailure );
}
}
/// <summary>
///
/// </summary>
protected override bool RequiresLayout
{
get
{
return false;
}
}
/// <summary>
///
/// </summary>
protected override void OnClose()
{
base.OnClose();
//Release any resource that were needed.
if ( _serviceLogging != null )
{
_serviceLogging.Dispose();
_serviceLogging = null;
}
}
// ------------------------------------------------------------------
#endregion
#region Private methods.
// ------------------------------------------------------------------
/// <summary>
///
/// </summary>
private void InitializeServiceConnection()
{
try
{
//Code any Service initialization needed for WebServiceAppender to work.
//This is called from ActiveOptions.
//Instantiate the WebService.
if ( _serviceLogging == null )
{
_serviceLogging = new Zeta.Logging.LoggingService();
_serviceLogging.LogCompleted +=
new Zeta.Logging.LogCompletedEventHandler(
_serviceLogging_LogCompleted );
// --
if ( !string.IsNullOrEmpty( url ) )
{
_serviceLogging.Url = url;
}
if ( timeoutSeconds > 0 )
{
_serviceLogging.Timeout = timeoutSeconds * 1000;
}
if ( useProxy )
{
WebProxy proxy = new WebProxy();
proxy.Address = new Uri( proxyUrl );
proxy.BypassProxyOnLocal =
proxyBypassOnLocal.GetValueOrDefault(
proxy.BypassProxyOnLocal );
proxy.UseDefaultCredentials =
proxyUseDefaultCredentials.GetValueOrDefault(
proxy.UseDefaultCredentials );
if ( !proxy.UseDefaultCredentials )
{
NetworkCredential credentials =
new NetworkCredential();
credentials.Domain = proxyCredentialsDomain;
credentials.UserName = proxyCredentialsUserName;
credentials.Password = proxyCredentialsPassword;
proxy.Credentials = credentials;
}
_serviceLogging.Proxy = proxy;
}
}
}
catch ( Exception exc )
{
// TODO: Add Service name to error message.
ErrorHandler.Error(
"Could not initialize the service",
exc,
ErrorCode.GenericFailure );
// TODO: Reset the Service object to a known state if needed.
}
}
/// <summary>
/// Event handler.
/// </summary>
private void _serviceLogging_LogCompleted(
object sender,
AsyncCompletedEventArgs e )
{
if ( e.Error != null )
{
ErrorHandler.Error(
"Unable to send loggingEvent to Logging Service",
e.Error,
ErrorCode.WriteFailure );
}
}
// ------------------------------------------------------------------
#endregion
#region Private variables.
// ------------------------------------------------------------------
private string apiKey;
private string url;
private int timeoutSeconds;
private bool useProxy = false;
private string proxyUrl;
private bool? proxyBypassOnLocal;
private bool? proxyUseDefaultCredentials;
private string proxyCredentialsDomain;
private string proxyCredentialsUserName;
private string proxyCredentialsPassword;
private Logging.LoggingService _serviceLogging = null;
private LevelRangeFilter filter;
// ------------------------------------------------------------------
#endregion
}
/////////////////////////////////////////////////////////////////////////
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.