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:


  
    

2 comments:

  1. why isnt the app.config visible

    ReplyDelete
  2. You shouldn't be loading the app.config directly, you should be using the file that gets copied to the output directory, it will be named something like "[myprogram].exe.config".

    ReplyDelete