0
votes

I'm trying to create a Windows Service which will use log4net to log all the service's actions. This service will be installed using Wix.

I've tried almost everything already but I'm still stuck in a problem. The service is installed and is running but log4net is not generating any log files (I'm writing to .txt files). Here is my files:

Inside App.config

<configSections>
    <!-- Log4net config section -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
  </configSections>

  <log4net debug="true">    
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="dd_MM_yyyy" />
      <preserveLogFileNameExtension value="true" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date{dd/MM/yyyy HH:mm:ss} [%thread] %level %property{log4net:HostName} - %property{log4net:UserHostAddress}: %logger - %message %newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Service1.cs

protected override void OnStart(string[] args)
{
    log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Service1));

    logger.Info("Service Started");

    this.timer = new Timer();
    this.timer.Elapsed += new ElapsedEventHandler(this.timerLog_Tick);
    this.timer.Interval = 6000;
    this.timer.Start();
}

WIX Configuration

<?xml version="1.0" encoding="UTF-8"?>

<?define ProductVersion="1.0.0.0" ?>
<?define UpgradeCode="{7E57F5D8-A768-4016-8E1F-9C01833B1E20}" ?>
<?define Manufacturer="Company" ?>
<?define ProductName="ProductName1" ?>
<?define SkuName="ProductName1" ?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">  
    <Product Id="*" Name="$(var.ProductName)" Language="1046" Version="$(var.ProductVersion)" Manufacturer="$(var.Manufacturer)" UpgradeCode="$(var.UpgradeCode)" Codepage="65001">

    <Package InstallerVersion="301"
                 Compressed="yes"
                 Languages="1046"
                 SummaryCodepage="1251"
                 Platform="x86" />

    <Media Id="1"
        Cabinet="$(var.SkuName).cab"
        EmbedCab="yes" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="CompanyFolder" Name="Company">
          <Directory Id="ProductDirectory" Name="$(var.ProductName)" />
        </Directory>
      </Directory>
    </Directory>

    <ComponentGroup Id="MainComponentGroup">
      <Component Directory="ProductDirectory">
        <File Name="$(var.My.Service.Test.TargetFileName)"
                      Source="$(var.My.Service.Test.TargetPath)"
                      KeyPath="yes"
                      Vital="yes" />
        <ServiceInstall Id="SeviceInstall"
                                Name="$(var.ProductName)"
                                DisplayName="$(var.ProductName)"
                                Type="ownProcess"
                                Interactive="no"
                                Start="auto"
                                Vital="yes"
                                ErrorControl="normal"
                                Account="LocalSystem">
        </ServiceInstall>
        <ServiceControl Id="ServiceControl_Start"
                                Name="$(var.ProductName)"
                                Start="install"
                                Wait="no" />
        <ServiceControl Id="ServiceControl_Stop"
                                Name="$(var.ProductName)"
                                Stop="both"
                                Remove="uninstall"
                                Wait="yes" />
      </Component>

      <Component Id="ProductDependecies" Directory="ProductDirectory" Guid="73D7C322-1E51-44AE-AB27-DCF72E238078">
        <File Name="My.Service.Test.exe.config"
                      Source="$(var.My.Service.Test.TargetDir)My.Service.Test.exe.config"
                      Vital="yes" />

        <File Name="log4net.dll"
                      Source="$(var.My.Service.Test.TargetDir)log4net.dll"
                      Vital="yes" />

        <File Name="log4net.xml"
                      Source="$(var.My.Service.Test.TargetDir)log4net.xml"
                      Vital="yes" />

      </Component>

    </ComponentGroup>

    <Feature Id="MainFeature"
                 Level="1">
      <ComponentGroupRef Id="MainComponentGroup" />
    </Feature>
  </Product>
</Wix>

When the service is installed these are in the folder:

  1. .config file
  2. log4net.dll and log4net.xml
  3. .exe file (service)

When I run the service using Visual Studio, the log is created inside the "Logs" folder correctly.

I tried giving permission on the installed folder to "Everyone", no success. I even tried to create the "Logs" folder and give permission to it, also didn't work.

I searched a lot and couldn't find nothing regarding wix + log4net. It seems to me that it is something with Wix but I really don't know what it is.

1

1 Answers

2
votes

I think the problem is that the current directory is not what you think it is. I believe if the application runs as a service the current directory is the system32 folder, but I did not verify this now.

If you try to use an absolute path (with correct permissions) in your configuration then it should work as expected.