Hi,

I've been trying to setup log4net to work with the .NET Compact Framework, but have had no luck.

Here are the details:
log4net 1.2.9 beta
Visual Studio .NET 2003
.NET Framework 1.1
.NET Compact Framework 1.0 SP3
Smartphone 2003 SDK
Pocket PC 2003 SDK

I'm developing with C# for the Smartphone 2003/Pocket PC 2003 platforms, and decided to try log4net because I wanted console output but console statements don't work with the Smartphone emulator.

I took a look at the C# example for .NET Compact Framework 1.0 included with log4net, and copied its .config file (ConsoleApp.exe.config) over to a simple Smart Device Application GUI test project called SmartDeviceApp.

I made a few changes:
- turned on internal debugging (according to http://logging.apache.org/log4net/release/faq.html#internalDebug) to see if it would help. - added the configSections element (according to http://logging.apache.org/log4net/release/manual/configuration.html#dot-config) because that was missing from the .config file I copied from the .NET CF example. - added an OutputDebugStringAppender, since I wanted to have the output show up in Visual Studio's Output window.

Here is what my SmartDeviceApp.exe.config file looks like:

<?xml version="1.0" encoding="utf-8" ?>

<!-- .NET application configuration file

The .NET Compact Framework does not support application configuration files, but log4net supports using configuration files with similar names and structure,
    to store the log4net configuration.
-->

<configuration >
   <!-- This section contains the log4net configuration settings -->
   <appSettings>
       <add key="log4net.Internal.Debug" value="true"/>
   </appSettings>
   <system.diagnostics>
       <trace autoflush="true">
           <listeners>
               <add
                   name="textWriterTraceListener"
                   type="System.Diagnostics.TextWriterTraceListener"
                   initializeData="C:\tmp\log4net.txt" />
           </listeners>
       </trace>
   </system.diagnostics>
   <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
   </configSections>
   <log4net>
       <!-- Define some output appenders -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
           <param name="File" value="LQSS-SS-log.txt" />
           <param name="AppendToFile" value="true" />

           <param name="MaxSizeRollBackups" value="10" />
           <param name="MaximumFileSize" value="100" />
           <param name="RollingStyle" value="Size" />
           <param name="StaticLogFileName" value="true" />

           <layout type="log4net.Layout.PatternLayout">
               <param name="Header" value="[Header]\r\n"/>
               <param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
           </layout>
       </appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
           <param name="File" value="log-file.txt" />
<!-- Example using environment variables in params -->
           <!-- <param name="File" value="${TMP}\\log-file.txt" /> -->
           <param name="AppendToFile" value="true" />
<!-- An alternate output encoding can be specified -->
           <!-- <param name="Encoding" value="unicodeFFFE" /> -->

           <layout type="log4net.Layout.PatternLayout">
               <param name="Header" value="[Header]\r\n"/>
               <param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" />
           </layout>
           <!-- Alternate layout using XML
           <layout type="log4net.Layout.XMLLayout" /> -->
       </appender>

<appender name="MulticastAppender" type="log4net.Appender.UdpAppender" >
           <param name="Threshold" value="WARN" />
               <param name="RemoteAddress" value="224.0.0.1" />
               <param name="RemotePort" value="8080" />
               <layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
               </layout>
           </appender>

<appender name="ConsoleAppender" 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>
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
           <layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
           </layout>
       </appender>

<!-- Setup the root category, add the appenders and set the default level --> <root>
           <level value="INFO" />
           <appender-ref ref="LogFileAppender" />
           <appender-ref ref="ConsoleAppender" />
           <appender-ref ref="MulticastAppender" />
           <appender-ref ref="OutputDebugStringAppender" />
           <!-- <appender-ref ref="A" /> -->
       </root>

       <!-- Specify the level for some specific categories -->
<logger name="SmartDeviceApp.Program">
           <!-- <appender-ref ref="B" /> -->
           <level value="ALL" />
           <appender-ref ref="RollingLogFileAppender" />
       </logger>
<logger name="SmartDeviceApp.Program">
           <!-- <appender-ref ref="B" /> -->
           <level value="ALL" />
           <appender-ref ref="OutputDebugStringAppender" />
       </logger>
</log4net>

</configuration>

I've added this file to my SmartDeviceApp project, and set the Compile Action for the file to "Content", just like the .config file for the ConsoleApp example so that it will get copied over properly. I've also added the .NET Compact Framework release version of log4net.dll as a reference.

Here's how the code looks like in my test project:

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using log4net;
using log4net.Config;

namespace SmartDeviceApp
{
   /// <summary>
   /// Summary description for Program.
   /// </summary>
   ///

   public class Program
   {

private static readonly ILog log = LogManager.GetLogger(typeof(Program));
       ... some classes/methods ...

       static void Main()
       {
           log4net.Config.XmlConfigurator.Configure();

           log.Debug("test");
           log.Error("test");
           log.Fatal("test");
           log.Info("test");
           log.Warn("test");
... some calls ...

           Application.Run(new Form1());
           LogManager.Shutdown();
       }
   }
}

I run the program and none of the logging statements appear. I have a strong suspicion that the XML .config file isn't even being processed at since the internal debugging section in the config file is supposed to create a log at C:\tmp\log4net.txt, but nothing gets generated when the program is run. I've also tried renaming the .config file to log4net.config and using the overloaded version of log4net.Config.XmlConfigurator.Configure() that takes a FileInfo, but no luck there either. I've also tried renaming the .config file to App.config, which results in Visual Studio complaining that "The file 'app.config' could not be code-signed." and the project won't build. I feel like I've exhausted all options to get this simple logging to work. :)

Can anybody out there help? Thanks.

Cheers,
Louis Ong

Reply via email to