I'd like to use the Topshelf instance name as part of the log4net log file name
Asked Answered
S

2

5

When creating an instance of a service using TopShelf I'd like to be able to access the service instance name(which may have been set on the command line during installation as a service; which means I don't have direct access to it) to be able to use that as a property for the log file name in Log4Net.

In the example code below we set various properties which are available to use for logging in the global context. I'd love to be able to set the Service instance name here also; but can't seem to be able to get access to it during host initialization.

Anyone any suggestions as to how I could access the service instance name value at run time with Topshelf.

The example below is part of a common function which all our services use to start a service using Topshelf.

public static TopshelfExitCode Run(Func<IConsumerController> controllerFactory,
    string serviceName,
    string serviceDescription)
{
    // Initialise the Global log4net properties so we can use them for log file names and logging when required.
    log4net.GlobalContext.Properties["custom-assembly"] = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);
    log4net.GlobalContext.Properties["custom-processname"] = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
    log4net.GlobalContext.Properties["custom-process-id"] = System.Diagnostics.Process.GetCurrentProcess().Id;
// WOULD LIKE ACCESS TO THE SERVICE INSTANCE NAME HERE
    var logFileInfo = new System.IO.FileInfo(".\\Log.config");
    log4net.Config.XmlConfigurator.Configure(logFileInfo);

    var host = HostFactory.New(r =>
    {
        var controller = controllerFactory();
        r.Service<ConsumerService>( () => new ConsumerService(controller));
        r.SetServiceName(serviceName);
        r.SetDescription(serviceDescription + " © XYZ Ltd. 2012");
        r.SetDisplayName(serviceDescription + " © XYZ Ltd. 2012");
        r.StartAutomatically();
        r.EnablePauseAndContinue();
        r.RunAsLocalSystem();
    });
    return host.Run();
}
Shupe answered 19/9, 2012 at 12:15 Comment(0)
E
4

The instance name is passed on the command line, you can access the command line arguments and pull it from there. This might take a little tweaking, but if you look at the ServiceInstaller you can see how we adjust the command path via registry editing once the service is installed.

Etheline answered 19/9, 2012 at 12:54 Comment(3)
Thanks Travis; I'll give that a try and see how it works out.Shupe
Hi Travis; that worked out fine thanks. I can parse the command line for the instance name from the command line. Thanks again for your expertise.Shupe
Not a problem, you can also hit up the mailing list if you have questions. groups.google.com/forum/#!forum/topshelf-discussEtheline
I
6

The HostSettings are passed to the service factory, which contains the InstanceName as a property. You should use that to initialize the log appender you want to add to log4net.

Isador answered 2/12, 2012 at 3:50 Comment(1)
To add a little detail...the HostSettings are passed to te "ConstructUsing" method on the ServiceConfigurator. You'll likely have code something like this: ` x.Service<ServiceRunner>(sc => { sc.ConstructUsing(hostSettings => new ServiceRunner(hostSettings)); `Tubercular
E
4

The instance name is passed on the command line, you can access the command line arguments and pull it from there. This might take a little tweaking, but if you look at the ServiceInstaller you can see how we adjust the command path via registry editing once the service is installed.

Etheline answered 19/9, 2012 at 12:54 Comment(3)
Thanks Travis; I'll give that a try and see how it works out.Shupe
Hi Travis; that worked out fine thanks. I can parse the command line for the instance name from the command line. Thanks again for your expertise.Shupe
Not a problem, you can also hit up the mailing list if you have questions. groups.google.com/forum/#!forum/topshelf-discussEtheline

© 2022 - 2024 — McMap. All rights reserved.