Asp.net

如何像 web.config 一樣轉換 log4net 配置?

  • June 5, 2017

從我的 .csproj 文件中:

<Content Include="log4net.config">
 <SubType>Designer</SubType>
</Content>
<Content Include="log4net.Release.config">
 <DependentUpon>log4net.config</DependentUpon>
</Content>
<Content Include="log4net.Debug.config">
 <DependentUpon>log4net.config</DependentUpon>
</Content>
<Content Include="log4net.Live.config">
 <DependentUpon>log4net.config</DependentUpon>   
</Content>
<Content Include="log4net.Demo.config">
 <DependentUpon>log4net.config</DependentUpon>   
</Content>  

在我的 .csproj 文件的底部:

 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
   <Target Name="AfterCompile" Condition="exists('log4net.$(Configuration).config')">
     <TransformXml Source="log4net.config"
       Destination="$(IntermediateOutputPath)$(TargetFileName).config"
       Transform="log4net.$(Configuration).config" />
     <ItemGroup>
       <AppConfigWithTargetPath Remove="log4net.config"/>
       <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
         <TargetPath>$(TargetFileName).config</TargetPath>
       </AppConfigWithTargetPath>
     </ItemGroup>
   </Target>

來自 log4net.config

<connectionString name="ConnName" 
value="Data Source=localhost\sqlexpress;Initial Catalog=localdb;Persist Security Info=True;Integrated Security=SSPI;" />

從 log4net.Live.config(刪除敏感數據)

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <connectionString name="ConnName" value="Data Source=127.0.0.1;Initial Catalog=DBName;Persist Security Info=True;User ID=userid;Password=pword"
       providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</configuration>

我檢查了 msbuild 輸出,發現它正確地轉換了我的 web.config,但我沒有看到它轉換 log4net 的輸出。此外,當我在發布後檢查 log4net.config 文件時,它具有原始連接字元串。

我究竟做錯了什麼 :)?

謝謝!

更新

我在 msbuild 輸出的程式碼中有一些錯誤,這些錯誤是我沒有看到的警告。我修復了這些,現在我從 MSBuild 獲得了一些輸出:

AfterCompile:轉換源文件:log4net.config 應用轉換文件:log4net.Live.config 輸出文件:obj\Live\Common.UI.Web.dll.config

轉換成功

這仍然是一個問題,因為文件應該命名為log4net.config,而不是Common.UI.Web.dll.config…

無論出於何種原因

$(目標文件名)

採用 .csproj 文件名的名稱。如果我只用 log4net 替換它,那麼它會正確輸出

更新

文件卡在 obj 文件夾中,發佈時沒有被拾取。

更新:對於 Visual Studio 2012(這些更新也適用於 VS2010)

在嘗試了許多不同的解決方案後,我深入研究了 web.Config 轉換是如何發生的……這是我發現的最優雅的解決方案。

首先從您的項目中排除您的 log4net.config 文件,除非您真正了解項目 XML,否則您最終可能會得到一個非常混亂的重複引用。不要刪除文件只是排除它們(我們將通過項目編輯器包括它們)。

現在解除安裝您的項目,然後對其進行編輯……或者您選擇進入 proj xml。確保您有一個導入 Microsoft.WebApplication.targets 的節點。如果您在一個 Web 項目中,它可能已為您添加…搜尋此節點

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />

一旦你有了這個節點,你只需要添加一個 ItemGroup 節點……

<ItemGroup>
 <WebConfigsToTransform Include="log4net.config">
   <DestinationRelativePath>log4net.config</DestinationRelativePath>
   <Exclude>False</Exclude>
   <TransformFileFolder>$(TransformWebConfigIntermediateLocation)\original</TransformFileFolder>
   <TransformFile>log4net.$(Configuration).config</TransformFile>
   <TransformOriginalFolder>$(TransformWebConfigIntermediateLocation)\original</TransformOriginalFolder>
   <TransformOriginalFile>$(TransformWebConfigIntermediateLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
   <TransformOutputFile>$(TransformWebConfigIntermediateLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
   <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
   <SubType>Designer</SubType>
 </WebConfigsToTransform>
 <None Include="log4net.Debug.config">
   <DependentUpon>log4net.config</DependentUpon>
 </None>
 <None Include="log4net.Release.config">
   <DependentUpon>log4net.config</DependentUpon>
 </None>
</ItemGroup>

我已經在 ItemGroup 中包含了依賴文件,雖然這不是必需的,但它可以將所有東西放在一起。請注意,您沒有創建新任務或目標,現在轉換的處理方式與 web.config 轉換完全一樣。

一位微軟員工在空閒時間添加了對所有文件執行此操作的功能,就像您可以使用的一樣web.config,在名為 SlowCheetah 的擴展程序中。查看評論SlowCheetah Xml Transformvsi 擴展下載

引用自:https://stackoverflow.com/questions/5557866