2015-10-20 65 views
0

我试图创建一个Windows服务,它将使用log4net记录所有服务的操作。该服务将使用Wix进行安装。Wix + log4net:安装Windows服务后未记录服务

我已经尝试了几乎所有的东西,但我仍然陷入了一个问题。该服务已安装并正在运行,但log4net未生成任何日志文件(正在写入.txt文件)。下面是我的文件:

里面的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配置

<?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> 

当服务安装这些文件夹中:

  1. config文件
  2. log4net.dll和log4net.xml
  3. .exe文件(服务)

当我使用Visual Studio运行服务时,正确地在“日志”文件夹内创建日志。

我试图给安装文件夹的权限为“Everyone”,没有成功。我甚至试图创建“日志”文件夹并给予它许可,也没有工作。

我搜索了很多,并且找不到关于wix + log4net的内容。在我看来,这是Wix的东西,但我真的不知道它是什么。

回答

2

我认为问题在于当前目录不是您认为的那样。我相信如果应用程序作为服务运行,当前目录是system32文件夹,但我现在没有验证。

如果您尝试在配置中使用绝对路径(具有正确的权限),那么它应该按预期工作。

+1

[Windows服务的当前目录不是您所期望的](http://haacked.com/archive/2004/06/29/current-directory-for-windows-service-is-not-what-you- expect.aspx /) – stuartd