I am writing windows mobile (6.5.3) application on MC9190 device (CF 3.5). I want to log certain events (e.g. user login info) and any runtime exceptions occurred. How can I do that? I tried to use log4net from this example http://breathingtech.com/2009/using-apache-log4net-in-net-compact-framework-projects/ But CF 3.5 is not supported. (http://logging.apache.org/log4net/release/framework-support.html). How can I log events/exceptions in my application? Is there any standard log framework available for CF 3.5? Thank you.
-
I am trying to see if NLog works as they do support CF 3.5. But the thing is, when I include logger in mobile application, it is not getting configured. All flags are false as if it is not able to read my NLog.config – sjj May 09 '13 at 20:45
-
How did you think log4net is not FC compatible: http://logging.apache.org/log4net/release/framework-support.html – josef May 10 '13 at 16:44
-
Microsoft .NET Compact Framework 3.5 is not listed there. I tried with log4net latest version, but I keep getting following errors : Error 1 The type 'System.Uri' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Error 2 The type 'System.Xml.XmlElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. – sjj May 10 '13 at 16:48
-
That will not be a problem. I would download the log4net source code and compile with CF3.5. OTOH you just have to add the missing references to your project. For System.Uri enusre you are referencing the compact framework DLLs/assemblies, see also my admin post here http://www.hjgode.de/wp/2009/10/31/itextsharp-running-on-compact-framework-windows-mobile/. USING CF2 assemblies is possible in CF3.5 except the assembly uses strct versioning of referenced system assemblies (bad programming behaviour). In the latter case you have to recompile the source. – josef May 10 '13 at 16:57
-
Thanks for your reply. I actually did think of compiling the source code for CF3.5. But I just couldn't figure out how to do so by looking into the build script. I really appreciate if you could guide me how I can build that for CF3.5. – sjj May 10 '13 at 17:34
1 Answers
Writing a simple logging is not that hard. Regardless which logging you implement, you need to add a line for each exception or event you want to log. Log4Net and other frameworks are full blown and support techniques that are not usable on Compact Framework (ie log to system event log). So you must decide on your own, if it is worth to use a framework where you cannot use most of the features supported in the full framwork.
There are also some more simple logging tools: http://blogs.msdn.com/b/davidklinems/archive/2006/08/17/704662.aspx
See also: Logging Library for .NET Compact Framework?
The problem with log4net and it's compact framework support is that is mostly not maintained over the time (as with other open source libs too).
Getting a working log4net for compact framework is easy:
- Open the log4.net src/log4net.vs2008.sln
- Add a new SmartDevice class library project (WM5 SDK and CF2 is OK) and name it log4netCF
- delete the existing class1.cs file
change the project properties:
Assembly name and Default namespace = "log4net"
Now just right click every source code folder in the log4net.vs2008 project in solution explorer and select copy and then right click log4netCF project and select paste Repeat that for every single folder:
- Appender,
- Config,
- Core,
- DateFormatter,
- Filter,
- Layout,
- ObjectRenderer,
- Plugin,
- Repository,
- Util
also copy the single files:
- AssemblyInfo.cs
- AssemblyVersionInfo.cs
- GlobalContext.cs
- ILog.cs
- LogicalThreadContext.cs
- LogManager.cs
- MDC.cs
- NDC.cs
- ThreadContext.cs
from log4net.vs2008 into the log4netCF project.
- delete Properties/AssemblyInfo.cs in log4netCF project
- add PocketPC;NETCF_2_0;NETCF to the build options of log4netCF
Now right click the log4netCF project and select Build. It should build without any error and you have a working Compact Framework 2.0 log4net assembly.
A dropin of the project file is available at www.hjgode.de/temp/log4netCF.csproj. Just download and place into your subversion copy of log4net into the src folder beside the log4net.vs2008.csproj file.
-
Thanks for your suggestion. I will definitely take a look at the logging tools. – sjj May 10 '13 at 17:35
-