Tuesday, June 24, 2014

Getting Log4Net integration with Topshelf working

I was exploring a bit on logging in Topshelf using Log4Net. It's quite easy to use once I figured out how to pass in the log config file and having the correct content in the config file. Below is an example showing how to set this up with the bare minimum to get the logging to work.

Topshelf program:

public class Program
    {
        public static void Main(string[] args)
        {

            HostFactory.Run(hostConfigurator =>
                {
                    hostConfigurator.UseLog4Net("..\\..\\App.config");

                    hostConfigurator.Service<bird>(serviceConfigurator =>
                        {
                            serviceConfigurator.ConstructUsing(name => new Bird());
                            serviceConfigurator.WhenStarted(nm => nm.Start());
                        });
                });
        }
    }

    public class Bird
    {
        public void Start()
        {
            HostLogger.Get<bird>().Info("Chirp, chirp!");
        }
    }


App.config: