Log4Net可以在本地开发人员架构中正常工作,也可以在工作角色中使用,但在Web角色中则不能 [英] Log4Net working fine in local dev fabric, worker role also, but not in Web Role

查看:109
本文介绍了Log4Net可以在本地开发人员架构中正常工作,也可以在工作角色中使用,但在Web角色中则不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的情况:

我已将自定义log4net Appender配置为写入Azure表存储.我有WorkerRole和WebRole.这两个角色都使用osFamily ="3"(Windows Server 2012),我都使用.NET 4.5,并将ASP.NET MVC4用于WebRole.一切都已配置并在本地正常运行-辅助角色和Web角色均正确记录.但是,当我部署到Azure时,只有Worker角色成功记录日志,而webrole却不会生成任何日志(任何日志!),

I have configured custom log4net Appender to write to Azure Table storage. I have WorkerRole and WebRole. Both roles use osFamily="3" (Windows Server 2012), I use .NET 4.5 in both and use ASP.NET MVC4 for the WebRole. Everything is configured and works fine locally - both the worker and the web role are logging correctly. However, when I deploy to Azure, only Worker role logs successfully, webrole does not produce any logs (any!), while it should.

我已经在WebRole上配置了log4net进行调试,并将log4net调试数据发送到Trace中以检查某些问题,但是它没有发送任何错误/警告.也不它发送日志消息.

I have configured log4net on the WebRole to debug and emit log4net debugging data into the Trace to check for some issues, but it sends no errors/warnings. Nor it sends logging messages.

这是log4net调试生成的日志(剥离了敏感数据):

Here is the log produced by log4net debugging (stripped sensitive data):

log4net:log4net程序集[log4net,版本= 1.2.11.0,文化=中性, PublicKeyToken = 669e0ddf0bb1aa2a].从加载 [D:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \临时ASP.NET 文件\ root \ 59a01799 \ 5b6b1a2 \ assembly \ dl3 \ 8d97587f \ b9d49402_c202ce01 \ log4net.dll]. (Microsoft Windows NT 6.2.9200.0上的.NET运行时[4.0.30319.18010]) log4net:defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] log4net:创建存储库 对于程序集[_my_referenced_assembly_,版本= 1.3.0.20282, 文化=中性,PublicKeyToken =空] log4net:程序集 [_my_referenced_assembly_,版本= 1.3.0.20282,文化=中性, PublicKeyToken = null]从加载 [D:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \临时ASP.NET 文件\ root \ 59a01799 \ 5b6b1a2 \ assembly \ dl3 \ 8cc98c20 \ 594c5b41_6f07ce01_my_referenced_assembly_.dll]] log4net:程序集[_my_referenced_assembly_,版本= 1.3.0.20282, 文化=中性,PublicKeyToken =空]没有 RepositoryAttribute规定. log4net:程序集 [_my_referenced_assembly_,版本= 1.3.0.20282,文化=中性, 使用存储库[log4net-default-repository]和PublicKeyToken = null]和 存储库类型[log4net.Repository.Hierarchy.Hierarchy] log4net: 使用类型创建存储库[log4net-default-repository] [log4net.Repository.Hierarchy.Hierarchy]已加载 "Microsoft.WindowsAzure.ServiceRuntime,版本= 1.8.0.0, 文化=中性,PublicKeyToken = 31bf3856ad364e35 来自ServiceRuntime的"_some_setting_1_":通过(_my_value1_).得到 来自ServiceRuntime的"_some_setting_2_":通过(_my_value2_).得到 来自ServiceRuntime的"_some_setting_3_":通过(_my_value3_).得到 来自的"Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" ServiceRuntime:通过(_my_conn_string_).得到"_some_setting_4_" 来自ServiceRuntime:通过(_my_value4_). log4net:创建存储库 对于程序集[_my_WEB_assembly_,版本= 1.3.0.20283,文化=中性, PublicKeyToken = null] log4net:程序集[_my_WEB_assembly_, 版本= 1.3.0.20283,文化=中性,PublicKeyToken =空]从加载 [D:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \临时ASP.NET 文件\ root \ 59a01799 \ 5b6b1a2 \ assembly \ dl3 \ f695181f \ 98c73242_6f07ce01_my_WEB_assembly_.dll] log4net:程序集[_my_WEB_assembly_,版本= 1.3.0.20283, 文化=中性,PublicKeyToken =空]没有 RepositoryAttribute规定. log4net:程序集[_my_WEB_assembly_, 版本= 1.3.0.20283,文化=中性,公钥令牌=空]使用 储存库[log4net-default-repository]和储存库类型 [log4net.Repository.Hierarchy.Hierarchy] log4net:存储库 [log4net-default-repository]已存在,使用存储库类型 [log4net.Repository.Hierarchy.Hierarchy]从中获取"acs:idps" ServiceRuntime:失败.从ConfigurationManager中获取"acs:idps": 通过([my_value]).

log4net: log4net assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [D:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\59a01799\5b6b1a2\assembly\dl3\8d97587f\b9d49402_c202ce01\log4net.dll]. (.NET Runtime [4.0.30319.18010] on Microsoft Windows NT 6.2.9200.0) log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] log4net: Creating repository for assembly [_my_referenced_assembly_, Version=1.3.0.20282, Culture=neutral, PublicKeyToken=null] log4net: Assembly [_my_referenced_assembly_, Version=1.3.0.20282, Culture=neutral, PublicKeyToken=null] Loaded From [D:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\59a01799\5b6b1a2\assembly\dl3\8cc98c20\594c5b41_6f07ce01_my_referenced_assembly_.dll] log4net: Assembly [_my_referenced_assembly_, Version=1.3.0.20282, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. log4net: Assembly [_my_referenced_assembly_, Version=1.3.0.20282, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] Loaded "Microsoft.WindowsAzure.ServiceRuntime, Version=1.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Getting "_some_setting_1_" from ServiceRuntime: PASS (_my_value1_). Getting "_some_setting_2_" from ServiceRuntime: PASS (_my_value2_). Getting "_some_setting_3_" from ServiceRuntime: PASS (_my_value3_). Getting "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" from ServiceRuntime: PASS (_my_conn_string_). Getting "_some_setting_4_" from ServiceRuntime: PASS (_my_value4_). log4net: Creating repository for assembly [_my_WEB_assembly_, Version=1.3.0.20283, Culture=neutral, PublicKeyToken=null] log4net: Assembly [_my_WEB_assembly_, Version=1.3.0.20283, Culture=neutral, PublicKeyToken=null] Loaded From [D:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\59a01799\5b6b1a2\assembly\dl3\f695181f\98c73242_6f07ce01_my_WEB_assembly_.dll] log4net: Assembly [_my_WEB_assembly_, Version=1.3.0.20283, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. log4net: Assembly [_my_WEB_assembly_, Version=1.3.0.20283, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] log4net: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy] Getting "acs:idps" from ServiceRuntime: FAIL. Getting "acs:idps" from ConfigurationManager: PASS ([my_value]).

这是我的app.config(用于工作人员)和web.config(用于网络角色)文件的示例(为防万一,我还将其设置为WaIISHost.config,但结果仍然相同-完全静音的WebRole ):

Here is sample of both my app.config (for worker) and web.config (for web role) files (I also set this into WaIISHost.config just in case, but the result is still the same - totally muted WebRole):

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    ...
  </configSections>
 ...
  <log4net>
    <appender name="AzureTableStoreAppender" type="_MyAssembly_.Logging.AzureTableStorageAppender, MyAssembly">
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="AzureTableStoreAppender" />
    </root>
  </log4net>
</configuration>

我通过组装级别属性配置log4net.因此,在工作者角色集会中,我有:

I configure log4net via the assembly level attribute. Thus in Worker role assembly I have:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

在网络角色中,我有:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

正如我所说,在本地运行时,一切正常,并且写表实体也很好.我的本地环境是Windows 8 Pro,它在IIS8(即IIS8)的IIS服务器(不是IIS Express)中运行Web角色.我还尝试将WebRoleexecutmentContext标记为elevated,但仍然没有任何反应.就像log4net的黑洞一样.

As I said, everything works fine and writes table entities fine when run locally. My Local Environemtn is Windows 8 Pro, running the web roles in IIS Server (not IIS Express), which is IIS8. I also tried marking the WebRole executionContext to elevated but still nothing happens. It is like a black hole for log4net.

我的自定义附加程序是在单独的Assembly.net Framework 4.5(完整版)中定义的,其类库类型在Web项目和Worker项目中都被引用.如前所述-在本地一切正常.肯定会加载定义我的自定义追加程序的程序集,因为它还包含我的Web应用程序的业务逻辑,该程序也可以部署在Azure中.

My custom appender is defined in a separate Assembly, .net Framework 4.5 (full), of type Class Library which is referenced in both Web and Worker projects. As already said - locally everything works fine. The assembly that defines my custom appender is definitely loaded, because it also contains business logic of my web app, which works deployed in Azure as well.

更新

有趣的发现.当我使用Debug构建配置时,所有内容都可以在任何地方使用(还可以使用Azure Web角色).但是,当我使用Release配置(包装和生产代码的默认设置)时,我似乎无法使log4net在任何地方都能正常工作.这意味着即使使用Release配置,我也无法使Log4Net甚至在本地运行.没有引发异常,没有记录错误.可能很奇怪...

Interesting discovery. When I use Debug build configuration everything works everywhere (azure web role also). But when I use Release configuration (which is default for packaging and production code) I can't seem to be able to make log4net working anywhere. Meaning that I can't make Log4Net running even locally with Release config. No exceptions are being raised, no errors being logged. As strange as it can be...

推荐答案

事实证明,Release版本引起了问题.根据 log4net的常见问题解答部分,通过程序集级属性配置log4net在DEBUGRELEASE配置中可能表现不同.

It turns out the Release build is causing issues. According to the log4net's FAQ section, configuring log4net via assembly level attribute may behave different in DEBUG and RELEASE configurations.

在我的设置中,我使用的是static readonly property(基本控制器的),在声明时进行了初始化.这似乎错误记录器配置.我将Logger实例移动到Web应用程序类型的instance property,并将控制器属性更改为仅返回应用程序的属性.现在,所有构建配置都可以正常工作.

In my setup I was using a static readonly property (of a base controller) initialized upon declaration. This seemed to bug the logger configuration. I moved my Logger instance to an instance property of my Web Application type and changed my controller property to just return Application's property. Now everything is working fine in all build configurations.

这篇关于Log4Net可以在本地开发人员架构中正常工作,也可以在工作角色中使用,但在Web角色中则不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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