【问题标题】:Log4Net web.config transform wont workLog4Net web.config 转换不起作用
【发布时间】:2017-09-29 13:10:43
【问题描述】:

当我发布到我的 Azure 应用程序时,我试图让 web.config 转换为 Log4Net 工作,但它永远不会改变。我在转换中还有其他几个项目,它们工作得很好。这就是我所拥有的

<log4net>
    <appender name="AdoNetAppender"  type="log4net.Appender.AdoNetAppender"  xdt:Locator="Match(name)">
      <connectionString value="Server=production server connection details here" xdt:Transform="Replace"/>
    </appender>
  </log4net>

谁能告诉我哪里出错了?

这是我的配置转换文件(已删除实时细节)

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

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <log4net>
    <appender name="AdoNetAppender"  type="log4net.Appender.AdoNetAppender"  xdt:Locator="Match(name)">
      <connectionString value="Server=server details here" xdt:Transform="SetAttributes"/>
    </appender>
  </log4net>

  <connectionStrings>
    <add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=liveversionhere;AccountKey=liveversionhere;EndpointSuffix=core.windows.net" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="AzureWebJobsStorage"  connectionString="DefaultEndpointsProtocol=https;AccountName=liveversionhere;AccountKey=liveversionhere;EndpointSuffix=core.windows.net"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="DefaultConnection" connectionString="Data Source=liveversionhere" providerName="System.Data.SqlClient"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="CHPDataContext" connectionString="Data Source=liveversionhere" providerName="System.Data.SqlClient"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="elmah" connectionString="Data Source=liveversionhere" providerName="System.Data.SqlClient"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

  <appSettings>
    <add key="ida:redirectUri" value="liveversionhere" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="ida:ClientId" value=liveversionhere  xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="ida:ClientSecret" value=liveversionhere"  xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
    <add key="ida:TenantId" value=liveversionhere  xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
    
  </appSettings>
  
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
   
  </system.web>
</configuration>

**更新

我已经更改了转换(见下文),但仍然无法正常工作。有没有人知道这里发生了什么?这应该很简单,但我发现自己浪费了我的时间只是为了让这个基本的东西真正起作用

<log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" xdt:Locator="Match(name)">
      <connectionString name="Log4NetConn" value="Server=productionsdetailshere" xdt:Locator="Match(name)" xdt:Transform="Replace"/>
    </appender>
  </log4net>

【问题讨论】:

  • 最终设法让它工作,我不得不替换整个 log4net 部分 连接字符串部分根本不起作用

标签: azure azure-web-app-service log4net web.config-transform


【解决方案1】:

根据你的描述,我这边做了一些测试。假设您通过 VS 部署 Web 应用程序,我通过“构建 > 配置管理器”创建一个名为 Release.Azure 的新配置,如下所示:

然后我添加了一个名为Web.Release.Azure.config的web配置文件,配置内容如下:

<log4net>
  <appender name="AdoNetAppender"  type="log4net.Appender.AdoNetAppender"  xdt:Locator="Match(name)">
    <connectionString value="Server=azure production server connection details here" xdt:Transform="SetAttributes"/>
  </appender>
</log4net>

注意:connectionString 部分下属性xdt:Transform 的值ReplaceSetAttributes 都可以按预期工作。

使用 VS 的发布向导,在“设置”下选择我的 azure 配置,如下所示,并将其部署到 azure Web 应用程序。

此外,我还尝试通过Kudu Console在我的Web应用程序的d:\home\site下创建一个applicationHost.xdt文件,重新启动我的azure网站,然后我发现转换可以工作。详情可参考here

更新:

applicationhost.xdt 如下所示:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" xdt:Locator="Match(path)">
    <log4net>
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"  xdt:Locator="Match(name)">
        <connectionString value="Server=production server connection details here" xdt:Transform="SetAttributes"/>
      </appender>
    </log4net>
  </location>
</configuration>

【讨论】:

  • 这是我使用的确切过程,但它从来没有工作过。我有几个“关键”部分可以很好地转换,只是不是 log4net
  • 这很奇怪。我在web.config 文件中使用开发连接字符串定义了log4net 部分,转换可以工作。您是否已将其发布到本地并检查了web.config 文件?此外,请尝试在您的网络应用程序下创建一个applicationHost.xdt 文件以缩小此问题。
  • 好的,我添加了一个 applicationHost.xdt 文件,它仍然是一样的。这个文件里面应该有什么吗?
  • 我更新了答案并添加了 applicationhost.xdt 文件的示例。如果转换无法在您的本地部署中工作,我假设您可以检查您的 web.config 或更新您的问题并添加您的 web.config 和转换文件,以便我们缩小此问题的范围。
  • 我已经安装了 SlowCheetah 插件,我可以预览我的转换,一切正常,我的正常连接字符串,我的密钥等。除了 Log4Net,它拒绝工作
【解决方案2】:

不得不替换整个 log4net 部分

连接字符串根本不起作用

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2019-04-24
    相关资源
    最近更新 更多