0
votes

I created a web service project that uses Log4Net to log to the event viewer and optionally to a .log file. Everything works great on my machine, when I try to deploy it on another machine it won't even log to the text file or the event viewer. The fact that it's not even writing to the text file makes me think it's not able to find the log4net.dll or something to that effect?

I'm also receiving an error message when it hits the noted line below in my global.asax:

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the and elements. The configuration section should look like:

section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /

I'm stumped, I have no idea why this isn't working, help!

Ran an exe compiled from the following code that adds the source to the eventlog in Windows, like I said, works fine on my machine:

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

End Module

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: </configSections>--> Yes it still works on mine with this commented out.

<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)> 

Global.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
1

1 Answers

0
votes

You are looking for you configuration in your app.config file. To change this you can use:

// Configure log4net using the .log4net file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

or

log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]

as reference:

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.