4 Replies - 3861 Views - Last Post: 03 January 2013 - 03:14 AM Rate Topic: -----

#1 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

log4net won't create log file

Posted 11 September 2012 - 11:56 PM

Hi, DIC.

I decided to add log4net logging to one of my libraries.

The log.config file is as shown here
<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <log4net>

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log4net.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="3MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    
  </log4net>
</configuration>


The project (console) code is also here

<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="log.config", Watch:=True)> 

Module Module1

    Sub Main()
        Dim testChild As New Child
        Console.WriteLine("OPEN = " & testChild.IsOpen)
        Console.WriteLine("CLOSE = " & testChild.IsClose)

        testChild.Open()
        Console.WriteLine("STATE = " & testChild.State)
        testChild.Close()
        Console.WriteLine("STATE = " & testChild.State)

    End Sub

End Module

Public MustInherit Class Parent
    Protected Shared ReadOnly _Log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

    Protected _State As String = "NONE"

    Public MustOverride ReadOnly Property State() As String

    Public MustOverride Function Open() As Boolean

    Public Function IsOpen() As Boolean
        If _State = "OPEN" Then
            Return True
        Else
            _Log.Error("neither OPEN/CLOSE")
            Return False
        End If
    End Function

    Public MustOverride Function Close() As Boolean

    Public Function IsClose() As Boolean
        If _State = "CLOSE" Then
            Return True
        Else
            _Log.Error("neither OPEN/CLOSE")
            Return False
        End If
    End Function
End Class

Public Class Child
    Inherits Parent

    Public Overrides ReadOnly Property State() As String
        Get
            Return _State
        End Get
    End Property


    Public Overrides Function Open() As Boolean
        _Log.Debug("OPEN")
        _State = "OPEN"
        Return True
    End Function

    Public Overrides Function Close() As Boolean
        _Log.Debug("CLOSE")
        _State = "CLOSE"
        Return True
    End Function
End Class


But when I debug the project, no log4net.txt is created.

This isn't my real library code but it's a close approximation.
Any idea what may be wrong? Is it the config file or the code?

Is This A Good Question/Topic? 0
  • +

Replies To: log4net won't create log file

#2 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: log4net won't create log file

Posted 15 September 2012 - 03:03 AM

no answer?
Was This Post Helpful? 0
  • +
  • -

#3 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: log4net won't create log file

Posted 15 September 2012 - 03:14 AM

i tried what worked on this DIC thread but nothing happened
Was This Post Helpful? 0
  • +
  • -

#4 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: log4net won't create log file

Posted 27 December 2012 - 12:32 AM

i'll see if this bumps this thread up
Was This Post Helpful? 0
  • +
  • -

#5 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: log4net won't create log file

Posted 03 January 2013 - 03:14 AM

finally fixed this!

1. rename log.config to App.config

2. add
log4net.Config.XmlConfigurator.Configure()
as first line in Sub Main

3. remove
<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="log.config", Watch:=True)>
as it is no longer needed

step 2 was absolutely necessary. log4net would not be turned on without it.
step 1 was creating the file that step 2 uses for configuration

This post has been edited by olibenu: 03 January 2013 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1