2015-10-20 132 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