【发布时间】:2015-04-22 19:08:36
【问题描述】:
我创建了一个 Web 服务项目,该项目使用 Log4Net 记录到事件查看器,也可以选择记录到 .log 文件。一切都在我的机器上运行良好,当我尝试在另一台机器上部署它时,它甚至不会登录到文本文件或事件查看器。它甚至没有写入文本文件的事实让我认为它无法找到 log4net.dll 或类似的东西?
当它在我的 global.asax 中点击下面指出的行时,我也会收到一条错误消息:
log4net:ERROR XmlConfigurator: 未能在应用程序的 .config 文件中找到配置部分“log4net”。检查 .config 文件中的 and 元素。配置部分应如下所示:
section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /
我很困惑,我不知道为什么这不起作用,求助!
运行从以下代码编译的 exe,将源添加到 Windows 中的事件日志中,就像我说的,在我的机器上运行良好:
Option Explicit On
Option Strict On
Imports System
Imports System.Diagnostics
Imports System.Threading
Module Module1
Sub Main()
If Not EventLog.SourceExists("LendingService") Then
EventLog.CreateEventSource("LendingService", "Application")
Console.WriteLine("Creating Event Source: LendingService")
Else
Console.WriteLine("LendingService events already defined to this system.")
End If
End Sub
结束模块
Web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ACEConfigFileFullPath" value="C:\somepath\Bin\ACEFile.xml" />
<add key="ILConfigFileFullPath" value="C:\somepath\Bin\IL.xml"/>
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>
<connectionStrings/>
<system.web>
<!--
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<namespaces>
<clear/>
<add namespace="System"/>
<add namespace="System.Collections"/>
<add namespace="System.Collections.Generic"/>
<add namespace="System.Collections.Specialized"/>
<add namespace="System.Configuration"/>
<add namespace="System.Text"/>
<add namespace="System.Text.RegularExpressions"/>
<add namespace="System.Linq"/>
<add namespace="System.Xml.Linq"/>
<add namespace="System.Web"/>
<add namespace="System.Web.Caching"/>
<add namespace="System.Web.SessionState"/>
<add namespace="System.Web.Security"/>
<add namespace="System.Web.Profile"/>
<add namespace="System.Web.UI"/>
<add namespace="System.Web.UI.WebControls"/>
<add namespace="System.Web.UI.WebControls.WebParts"/>
<add namespace="System.Web.UI.HtmlControls"/>
</namespaces>
</pages>
<identity impersonate="true" />
</system.web>
</configuration>
log4net.config: --> 是的,它仍然适用于我的注释掉。
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="ApplicationName" value="Lending Service" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %newline %exception" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="FATAL"/>
<file value="webLog.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
</root>
<logger name="LendingService.Global_asax">
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</logger>
<logger name="LendingService.LendingService">
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
AssemblyInfo.vb:
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="log4net.config", Watch:=True)>
全球.asax:
Imports System.Web.SessionState
Imports IntelliLenderBUClasses
Public Class Global_asax
Inherits System.Web.HttpApplication
'Public objApplicant As New IntelliLenderBUClasses.ApplicantBU
Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
'ACE
Dim sACEPath As String
sACEPath = System.Configuration.ConfigurationManager.AppSettings("ACEConfigFileFullPath")
AceClientNet.BaseConfig.InitializeBU(sACEPath)
'IL
Dim sILPath As String
sILPath = System.Configuration.ConfigurationManager.AppSettings("ILConfigFileFullPath")
IntelliLenderBUClasses.BUBaseConfig.InitializeBU(sILPath)
'LOGGING
log4net.Config.XmlConfigurator.Configure() 'Error message happening here
log.Info("LendingService Application Started")
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim sendString As String = sender.ToString
log.Error("LendingService Application Error: " & Server.GetLastError.Message)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
log.Info("LendingService Application Ending")
End Sub
End Class
【问题讨论】:
标签: vb.net web-services logging log4net