log4Net

P

Patrick

I haven't used log4Net for a while, and couldn't figure out how to use the
Appender!

I have my App.config set as stated at the end of this post

Within the code, I did the following, but when I actually do logging like
log.Info("Application [testApp] Start"); (I only get the log message in the
Console window, and not in the file c:\log-file.txt as specified). the code
is executed as a console application by someone logged on as a local
Administrator on Windows XP Professional SP1, .NET framework 1.1

How could I get the Appender working??

------------------------------Start of Code
snippet------------------------------
// Create a logger for use in this class
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(typeof(LoggingExample));

/// <summary>
/// Static constructor used to initialise the logging before
/// the main class loads, i.e. before the Main() method is called.
/// </summary>
static LoggingExample()
{
// If using the following line, I get an error in console saying
// log4net:ERROR No appender named [FileAppender] could be found.
// log4net:ERROR No appender named [EventLogAppender] could be found.
//
//log4net.Config.DOMConfigurator.Configure();
log4net.Config.BasicConfigurator.Configure();
}
------------------------------End of Code
snippet------------------------------



------------------------------Start of
App.config------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<appender-ref ref="FileAppender" />
<appender-ref ref="EventLogAppender" />
<applicationName value="MyApp" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] -
%message%newline" />
</layout>
</appender>

<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\\log-file.txt" />
<appendToFile value="true" />
<encoding value="unicodeFFFE" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] -
%message%newline" />
</layout>
</appender>

</log4net>
</configuration>
------------------------------End of
App.config------------------------------
 
S

Steven Cheng[MSFT]

Hi Patrick,

From your description ,you seems have got some problems when trying to use
the log4net's FileAppender to write info into file logs. I'm also not very
familiar with the log4net, but after some general tests, it seems that
the
log4net.Config.BasicConfigurator.Configure();
dosn't work and currently I can get the fileappender work via intializing
by
log4net.Config.DOMConfigurator.Configure();

Here is my test app's code and confile file, you may have a look to see
whether it helps:

class MainApp
{
private static readonly ILog log = log4net.LogManager.Root;

public MainApp()
{


}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure();


if(log.IsInfoEnabled)
{
log.Info("logging start!");
}

SimpleClass sc = new SimpleClass();
sc.SayHello();


Console.Read();
}


}


app.config
=====================
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net>
<appender name="B" type="log4net.Appender.FileAppender">
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>
<appender name="A" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>

<!-- Setup the root category, add the appenders and set the default level
-->
<root>
<level value="DEBUG" />
<appender-ref ref="A" />
<appender-ref ref="B" />
</root>


</log4net>
</configuration>

==================================

Also, I suggest you try looking at some other public discussions or formus
to see whether they'll provide some further experiences on using this
component. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
P

Patrick

I figured out why when I use log4net.Config.BasicConfigurator.Configure(),
nothing is logged to the File using the FileAppender as specified in the
App.config- it is because the code just use the ConsoleAppender!

But when I use log4net.Config.DOMConfigurator.Configure(), it reads from the
App.config, I figured out with debugging the log4net source that in the
code, that it is failing on log4net's call to FindAppenderByReference!
Nothing seems to be initialising m_appenderBag, a Hashtable of Appenders?
Am I using the Appender, or initialising the Logger correctly??

Please see code enclosed!

Steven Cheng said:
Hi Patrick,

From your description ,you seems have got some problems when trying to use
the log4net's FileAppender to write info into file logs. I'm also not very
familiar with the log4net, but after some general tests, it seems that
the
log4net.Config.BasicConfigurator.Configure();
dosn't work and currently I can get the fileappender work via intializing
by
log4net.Config.DOMConfigurator.Configure();

Here is my test app's code and confile file, you may have a look to see
whether it helps:

class MainApp
{
private static readonly ILog log = log4net.LogManager.Root;

public MainApp()
{


}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure();


if(log.IsInfoEnabled)
{
log.Info("logging start!");
}

SimpleClass sc = new SimpleClass();
sc.SayHello();


Console.Read();
}


}


app.config
=====================
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net>
<appender name="B" type="log4net.Appender.FileAppender">
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>
<appender name="A" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>

<!-- Setup the root category, add the appenders and set the default level
-->
<root>
<level value="DEBUG" />
<appender-ref ref="A" />
<appender-ref ref="B" />
</root>


</log4net>
</configuration>

==================================

Also, I suggest you try looking at some other public discussions or formus
to see whether they'll provide some further experiences on using this
component. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
P

Patrick

P.S. Using Log4Net Version 1.1.1, which can be downloaded from
http://prdownloads.sourceforge.net/log4net/log4net_1_1_1.zip?download


Patrick said:
I figured out why when I use log4net.Config.BasicConfigurator.Configure(),
nothing is logged to the File using the FileAppender as specified in the
App.config- it is because the code just use the ConsoleAppender!

But when I use log4net.Config.DOMConfigurator.Configure(), it reads from the
App.config, I figured out with debugging the log4net source that in the
code, that it is failing on log4net's call to FindAppenderByReference!
Nothing seems to be initialising m_appenderBag, a Hashtable of Appenders?
Am I using the Appender, or initialising the Logger correctly??

Please see code enclosed!

Steven Cheng said:
Hi Patrick,

From your description ,you seems have got some problems when trying to use
the log4net's FileAppender to write info into file logs. I'm also not very
familiar with the log4net, but after some general tests, it seems that
the
log4net.Config.BasicConfigurator.Configure();
dosn't work and currently I can get the fileappender work via intializing
by
log4net.Config.DOMConfigurator.Configure();

Here is my test app's code and confile file, you may have a look to see
whether it helps:

class MainApp
{
private static readonly ILog log = log4net.LogManager.Root;

public MainApp()
{


}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure();


if(log.IsInfoEnabled)
{
log.Info("logging start!");
}

SimpleClass sc = new SimpleClass();
sc.SayHello();


Console.Read();
}


}


app.config
=====================
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net>
<appender name="B" type="log4net.Appender.FileAppender">
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>
<appender name="A" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />
</layout>
</appender>

<!-- Setup the root category, add the appenders and set the default level
-->
<root>
<level value="DEBUG" />
<appender-ref ref="A" />
<appender-ref ref="B" />
</root>


</log4net>
</configuration>

==================================

Also, I suggest you try looking at some other public discussions or formus
to see whether they'll provide some further experiences on using this
component. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thanks for your followup Patrick.

I've got the attached test app and will do some tests on my side. I'll
update you if I get some further progress on this. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
P

Patrick

That is strange.

I got the code tried out on two seperate developer workstations running
- WinXP Pro SP1
- .NET Framework 1.1
- Visual Studio .NET Enterprise Edition 2003
- log4net 1.1.1

On both machines, I get the following error showing up on console:
log4net:ERROR No appender named [A] could be found.
log4net:ERROR No appender named could be found.
log4net:ERROR No appenders could be found for category (root).
log4net:ERROR Please initialize the log4net system properly.

The only thing I had to do to get the Visual studio project/solution/c#
source file compiling is to alter the reference to log4net.dll to
c:\log4net_1_1_1\bin\log4net.dll

How did you "install" it? I
1) downloaded the logrnet_1_1_1.zip from
http://prdownloads.sourceforge.net/log4net/log4net_1_1_1.zip?use_mirror=ovh
2) Extracted zip file to c:\
3) Add reference to c:\ c:\log4net_1_1_1\bin\log4net.dll to the Visual
Studio project you posted
4) Compiled the testApp project

It seems weird, cause if I instead of adding reference to dll, I
1) Add project c:\log4net_1_1_1\src\log4net.csproj to the VS solution
2) Add project reference to log4net.csproj (instead of log4net.dll)
3) recomplie
4) As I debug, I found out that on the following line 421 of
Config\DOMConfigurator.cs,
4.1) a quick watch on
appenderRef.OwnerDocument.GetElementsByTagName("appender").Count returns 0!
4.2) a quick watch on appenderRef.OwnerDocument.OuterXml returns ""
4.3) a quick watch on appenderRef.paretnNode.OuterXml returns
"<root>\r\n<level value=\"DEBUG\" />\r\n<appender-ref ref=\"A\"
/>\r\n<appender-ref ref=\"B\" />\r\n</root>"

Line 421: appenderRef.OwnerDocument.GetElementsByTagName(APPENDER_TAG)

I don't understand why? We got the same code, this surely is just XML, why
is mine not reading the XML properly?? Did you have to do anything else
other other than compiling the sourcecode and app.config you posted?
 
J

Jens Ansorg

Steven said:
Anyway, this time I've attached the whole TestApp's solution in this
message(including the binary built exe). You may try running directly on
your machine to see whether it works.

hello,
I do have the same problems as Patrick.

I downloaded your zip and run it and what I get is

log4net:ERROR No appender named [A] could be found.
log4net:ERROR No appender named could be found.
log4net:ERROR No appenders could be found for category (root).
log4net:ERROR Please initialize the log4net system properly.


this is with
- log4net-1.1.1,
- .NET 1.1.4322
- W2K SP3

A colleague of mine works on a project and he uses log4net sucessfully.
I tried to continue work on this project but could not get the logger to
run on my machine. The only difference between his and mine machine is
that he is runing winXP SP2 instead of 2K. We could not find any other
differences regarding.NET


Any help apreciated


thanks
Jens Ansorg
 
P

Patrick

Tried the .exe, and I still get the same error!

I cannot think why. It seems to be an XML problem as I stated before. Why
is that
1) a quick watch on
appenderRef.OwnerDocument.GetElementsByTagName("appender").Count returns 0!
2) a quick watch on appenderRef.OwnerDocument.OuterXml returns ""
3) a quick watch on appenderRef.paretnNode.OuterXml returns
"<root>\r\n<level value=\"DEBUG\" />\r\n<appender-ref ref=\"A\"
/>\r\n<appender-ref ref=\"B\" />\r\n</root>"

Could there be any "environmental" problem with the config file reading or
XML reading?? I don't see how as your zip file contains the whole solution
structure including the exe and the config file in the directory where the
exe file is!
 
S

Steven Cheng[MSFT]

Hi Patrick,

Really strange, I'm also at a loss. Yes, not I'm sure there must be some
enviromental issue. Anyway, I'll also try testing on some other machine.
Also, if you find any new clues, please feel free to post here. Thanks.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
P

Patrick

I have absolutely no idea why, but if I use log4net1.2.0beta8 instead of
log4net1.1.1, it works!
 
P

Patrick

Now that I got log4net working with the "latest" version at
http://prdownloads.sourceforge.net/log4net/log4net-1.2.0-beta8.zip?download,
I try to use Log4Net from .NET Interop, it doesn't quite work out!

What I have is a .NET web-service proxy client (signed with strong name key)
that is exposed to ASP 2.0 (Classic) using
gacutil -i myClient.dll
regasm /tlb:myClient.tlb myclient.dll

It has all been working until I added in the following line to my Order.cs
class
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger("Order");

The code snippet for my Order class and iOrder Interface is as follows.

Note, I am trying this on a WinXP Pro SP1 workstation, with .NET Framework
1.1 and IIS 5.1

------------Start of Order.cs------------


//
// This source code was auto-generated by wsdl, Version=1.1.4322.573.
//
using System.Diagnostics;
using System.Configuration;
using System.Xml.Serialization;
using System;
using System.IO;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
using System.Runtime.InteropServices;
using Microsoft.Web.Services2.Security;
using Microsoft.Web.Services2.Security.Tokens;
using Microsoft.Web.Services2.Security.X509;

namespace MyOrg.web.publications
{
/// <summary>
/// Web service proxy client
/// for invoking the iPublisherData iOrder webservice of Two Ten
Communications
/// </summary>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="iOrderSoap",
Namespace="http://Publisher/webservices/")]
public class Order : Microsoft.Web.Services2.WebServicesClientProtocol ,
IOrder
{

/// <summary>
/// Logger to use for Log4Net
/// </summary>
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger("Order");


public Order()
{
this.Url = "http://publisher/iOrder.asmx;

//log4net.Config.DOMConfigurator.Configure();
}


//For demonstrating the log4net problem, the what the webservice takes in
is irrelevant

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://Publisher
/webservices/PlaceOrder", RequestNamespace="http://Publisher/webservices/",
ResponseNamespace="http://Publisher/webservices/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void PlaceOrder([MarshalAs(UnmanagedType.IUnknown)] Object order)
{

try
{

//For demonstrating the log4net problem, the what the webservice takes
in is irrelevant
this.Invoke("PlaceOrder", new object[] {order});
}
catch (SoapException soapExceptions)
{
//log.Fatal("Error with PlaceOrder",soapExceptions);
return "";
}
catch (Exception otherExceptions)
{
//log.Fatal("Error with PlaceOrder",otherExceptions);
return "";
}
}

}
}

------------End of Order.cs------------

------------Start of IOrder.cs------------
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.Web.Services;
using System.Runtime.InteropServices;



namespace MyOrg.web.publications
{

[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)]
public interface IOrder
{



[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://publisher
/webservices/PlaceOrder", RequestNamespace="http://publisher/webservices/",
ResponseNamespace="http://publisher/webservices/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
void PlaceOrder([MarshalAs(UnmanagedType.IUnknown)] Object order) ;



}

}



------------End of IOrder.cs------------
 
S

Steven Cheng[MSFT]

Hi Patrick,

Thanks for your followup. As you mentioned that when you try consuming the
webservice via .net interop in unmanaged code, you can't get it work. Have
you tried testing it in a simple VB6 app which only contains the webservice
call and no other codes? Also, I think you can also have a look in the
LOG4NET's source project or its communitry to see whether this is any known
issue on using it across interop. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top