如何改造log4net的配置类似的web.config? [英] How to transform log4net config like web.config?

查看:237
本文介绍了如何改造log4net的配置类似的web.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的.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文件发布之后,它具有原始连接字符串。

I checked msbuild output and I see that it transformed my web.config correctly, but I see no output for it transforming log4net. Also, when I check the log4net.config file after publish, it has the original connection string.

我是什么做错了:)

谢谢!

更新

我曾在code,它的MSBuild被输出作为我没有看到警告一些错误。
我固定的,现在我得到的MSBuild一些输出:

I had some errors in the code that msbuild were outputting as warnings that I didn't see. I fixed those and now I get some output from MSBuild:

AfterCompile:转变源
  文件:log4net.config
      应用转换文件:log4net.Live.config
      输出文件:OBJ \\现场\\ Common.UI.Web.dll.config结果
  转型成功

AfterCompile: Transforming Source File: log4net.config Applying Transform File: log4net.Live.config Output File: obj\Live\Common.UI.Web.dll.config
Transformation succeeded

这仍然是一个问题,因为文件应该命名为log4net.config,不Common.UI.Web.dll.config ...

This is still a problem, because the file should be named log4net.config, not Common.UI.Web.dll.config...

无论出于何种原因

$(TargetFileName)

$(TargetFileName)

正在上的.csproj文件名的名称。如果我只用log4net的替换它,然后它输出正确

is taking on the name of the .csproj file name. If I replace it with just log4net, then it outputs correctly

更新

文件是停留在文件夹中的obj和发布时,没有得到回升。

File is stuck in obj folder and is not getting picked up when publishing.

推荐答案

最后使用的 http://mint.litemedia.se/2010/01/29/transforming-an-app-config-file/ 为应用程序配置和log4net的配置。作品非常漂亮。

Ended up using http://mint.litemedia.se/2010/01/29/transforming-an-app-config-file/ for both the app config and the log4net config. Works very nicely.

有关log4net的配置,它添加到的csproj:

For the log4net config, add this to the csproj:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Target Name="ApplyConfiguration" Condition="Exists('log4net.$(Configuration).config')">  
       <XslTransformation XmlInputPaths="log4net.config" XslInputPath="log4net.$(Configuration).config" OutputPaths="log4net.config_output" />  
       <Copy SourceFiles="log4net.config_output" DestinationFiles="log4net.config" />  
   </Target>  
   <Target Name="BeforeBuild">  
       <CallTarget Targets="ApplyConfiguration"/>  
   </Target> 

这篇关于如何改造log4net的配置类似的web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆