Using log4net remote appender to centralize client error logging

Update: this post is trying to make log4net remote sink look like a WCF Service, I didn’t feel comfortable to deal with dotnet remoting running on NTService. Actually the remoting itself is quite stable and very easy to setup. I will doc it separately later.

We are building SOA based WPF app, usually clients don’t have access to database. When exception at client side occurs, we can use log4net eventLogApender to record the error, but a better solution should be collecting those errors at a central place, like a database table. Log4net RemoteAppender is a perfect fit here.

Unfortunately, the standard configuration doesn’t support WCF yet, I did some investigate about dotnet remoting. Later I simply created a web app with global.aspx.cs looks like this:

    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
             log4net.Config.XmlConfigurator.Configure((new System.IO.FileInfo(SystemInfo.ApplicationBaseDirectory + "\\Log4Net.config")));
             log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Log an info level message
            if (log.IsInfoEnabled) log.Info("Application [RemotingServer] Start");

            // Configure remoting. This loads the TCP channel as specified in the .config file.
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            // Publish the remote logging server. This is done using the log4net plugin.
            log4net.LogManager.GetRepository().PluginMap.Add(new log4net.Plugin.RemoteLoggingServerPlugin("LoggingSink"));

        }

The client side:

  <appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
    <!-- The remoting URL to the remoting server object -->
    <sink value="tcp://localhost:8085/LoggingSink" />
    <!-- Send all events, do not discard events when the buffer is full -->
    <lossy value="false" />
    <!-- The number of events to buffer before sending , had to change it to 1 in wpf app -->
    <bufferSize value="1" />
    <!-- Do not store event data that is slow to generate -->
    <onlyFixPartialEventData value="true" />
    <!-- Specify an evaluator to send the events immediatly under
			     certain conditions, e.g. when an error event ocurrs -->
    <!--
			<evaluator type="log4net.Core.LevelEvaluator">
				<threshold value="ERROR"/>
			</evaluator>
			-->
  </appender>

At the remote loggingSink side, we can just add one AdoNetAppender, which will direct all the client side logging into database.

One issue left, this global.asax.cs won’t auto trigger by iisreset, or client calling, I have to create a dummy WCF service then every time do a page refresh on that svc file to trigger it for now. Some simple simple missing here.

According to this post, upgrading a dotnet remoting to WCF service shouldn’t be too difficult, but does log4net RemoteLoggingServerPlugin support it?

Advertisements

5 thoughts on “Using log4net remote appender to centralize client error logging

  1. Pingback: UnhandledExceptionDispatcher for WPF app « maonet technotes

  2. My brother suggested I may like this blog. He was
    entirely right. This post truly made my day. You can not consider
    simply how much time I had spent for this information!
    Thank you!

    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here
    click here

  3. Thanks for posting this article! It lacked some information that would have helped me out but it pointed me in the right direction. 🙂

    What wasn’t apparent from the article is “RemotingConfiguration” is defined in System.Runtime.Remoting.

    Thanks!

  4. Nice article on remote logging, if it is possible to log all client log(in case of windows application) in a centralised server using log4net. Also to implement this do we need to done anything in server machine like application that subscribes log4net logs and printing in the log file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s