0

I am having an issue with log4net. I am working on a service application which I have been developing as a console app with DLL's. The dll's are self contained and the console app just calls a start method on the service to allow me to quickly swap out for a windows service for deployment. (don't have rights to install a service on my dev box but have full admin on the server, its a security nightmare where I work so don't ask).

When starting the console app on the server log4net functions just fine. When starting the windows service I get this error in the log4net log:

log4net: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]
no configuration section <common/logging> found - suppressing logging output

I am also using quartz in another part of the project so Common.Logging.dll is in the folder but adding the config sections for Common.Logging to use log4net did not fix the issue. From what I can see the only difference between the console app and service is the output type. Both the console app and the service have the same packages.config whicn only include log4net and the same references which only reference the log4net dll and the main project dll.

Any help tracking down where to look would be greatly appreciated.

EDIT: I am using the same app.config for both the console app and the windows service named the proper program.exe.config.

  • Could it be that the account under which the service runs (probably "Local Service") has no privilege to write to the log file's folder? What happens if you change "Local Service" (default, safe account) with "Local System" (old, unsafe account)? Or if you delete old log files and allow service under "Local Service" to create its own log file? – Dialecticus Jan 19 '15 at 16:38
  • @Dialecticus The issue was that log4net was not even initializing to try and write to the log. The error message from the internal log4net debug log was at least clear on that part. – RubberChickenLeader Jan 19 '15 at 16:49

1 Answers1

1

So after trying several things I replaced the Windows Service template project with a standard console app. Using the code from here https://stackoverflow.com/a/7764451/3302585

(Copied in case the link dies)

public static class Program
{
    #region Nested classes to support running as service
    public const string ServiceName = "MyService";

    public class Service : ServiceBase
    {
        public Service()
        {
            ServiceName = Program.ServiceName;
        }

        protected override void OnStart(string[] args)
        {
            Program.Start(args);
        }

        protected override void OnStop()
        {
            Program.Stop();
        }
    }
    #endregion

    static void Main(string[] args)
    {
        if (!Environment.UserInteractive)
            // running as service
            using (var service = new Service())
                ServiceBase.Run(service);
        else
        {
            // running as console app
            Start(args);

            Console.WriteLine("Press any key to stop...");
            Console.ReadKey(true);

            Stop();
        }
    }

    private static void Start(string[] args)
    {
        // onstart code here
    }

    private static void Stop()
    {
        // onstop code here
    }
}

and adding a service installer (http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx to understand what was going on then copying the Initialize Component code from the Windows Service Template project for the service installer) the project now runs as expected.

I am still not sure what is happening as the code appears 98% the same but if I had to guess it would be some sort of difference that is not obvious to a beginner to the differences between the Console App template and the Windows service template.

Community
  • 1
  • 1