SharpCrafters Forum – Not logging

SharpCrafters Support Forum

        


Not logging Expand / Collapse
Author
Message
Posted 8/18/2008 1:47:15 PM


Community Member
I tried the code from the demo in a project I created. I didn't copy paste all of it. Just a bit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Log4PostSharp;

namespace PostSharpLoggingC
{
   class Program
   {
       static void Main(string[] args)
       {
           int sum = Add(1, 2);
           Console.WriteLine(sum);
           Console.ReadLine();
       }

       [Log(EntryLevel = LogLevel.Debug, EntryText = "Adding {@i1} to {@i2}.", ExitLevel = LogLevel.Debug, ExitText = "Result of addition is {returnvalue}.")]
       private static int Add(int i1, int i2)
       {
           return i1 + i2;
       }
   }
}


and this in the app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>
 <log4net>
   <appender name="MainAppender" type="log4net.Appender.ConsoleAppender">
     <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
     </layout>
   </appender>
   <root>
     <level value="ALL" />
     <appender-ref ref="MainAppender" />
   </root>
 </log4net>
</configuration>


It compiles just fine and I can see the added IL-Code in reflector. AS you can see here

function Program.Add(i1: Integer; i2: Integer): Integer;
begin
   if (Program.~log4PostSharp~isDebugEnabled) then
   begin
       ~args~2 := New(array[2] of TObject, ( ( i1, i2 ) ));
       Program.~log4PostSharp~log.DebugFormat(CultureInfo.InvariantCulture, 'Adding {0} to {1}.', ~args~2)
   end;
   CS$1$0000 := (i1 + i2);
   ~returnValue~1 := CS$1$0000;
   if (Program.~log4PostSharp~isDebugEnabled) then
   begin
       ~args~3 := New(array[1] of TObject, ( ( ~returnValue~1 ) ));
       Program.~log4PostSharp~log.DebugFormat(CultureInfo.InvariantCulture, 'Result of addition is {0}.', ~args~3)
   end;
   begin
       Result := ~returnValue~1;
       exit
   end
end;


But it doesn't output the log things.

However f I add this

log4net.Config.BasicConfigurator.Configure();
           log4net.ILog log = log4net.LogManager.GetLogger("logger-name");
           log.Debug("test");
           Console.ReadLine();


I get the one line (nothing from log4postsharp) So I guess the configuration of log4net is correct but Log4postsharp is not picking it up.

What am I doing wrong?

LessThanDot
Post #3322
Posted 8/18/2008 8:56:41 PM


Michal Dabrowski

Community Member
The last of the snippets you've posted (the one using the Configurator) is very important. Not configured log4net will not do anything. Actually if you use the BasicConfigurator, the data in your app.config is ignored. You need to use XmlConfigurator to make log4net use the app.config file.

Now, I think that putting the XmlConfigurator attribute into AssemblyInfo.cs file (as opposed to configuring log4net "manually", by a method call) may solve the problem. Unfortunately I won't be able to check this myself until tomorrow evening, but if you want then you can put the following line:

[assembly: XmlConfigurator(Watch = true)]


into your AssemblyInfo.cs file and see if this helps (remember to first remove other references to log4net configurators from your code).

I suspect now that the problem is caused by the way the Log4PostSharp tries to speed up accessing log4net configuration - it caches parts of it. And I guess that in your case the data gets cached before log4net has chance to be configured (so the data that goes into the cache is the default "do not log anything"). Using the XmlConfigurator attribute should work before I can fix this bug.
Post #3323
Posted 8/19/2008 10:50:37 AM


Community Member
Yep that worked.

I will be making a blogpost about this in a hou or so.
But this time in VB.Net.

You can find it at <!-- m --><a class="postlink" href="http://blogs.lessthandot.com">http://blogs.lessthandot.com</a><!-- m -->
Post #3324
Posted 8/30/2008 2:06:57 PM


Community Member
Hi,

I am facing the same problem i.e nothing is getting logged. I am directly running the demo application. The application assembly info file even has the XmlConfigurator attribute for the Log4net.

My IL code in the reflector looks something like this:


.class public auto ansi beforefieldinit Program
   extends [mscorlib]System.Object
{
   .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
   {
       .maxstack 8
       L_0000: ldarg.0
       L_0001: call instance void [mscorlib]System.Object::.ctor()
       L_0006: ret
   }

   .method private hidebysig static int32 Add(int32 i1, int32 i2) cil managed
   {
       .custom instance void [Log4PostSharp]Log4PostSharp.LogAttribute::.ctor() = { EntryLevel=int32(1) EntryText=string('Adding {@i1} to {@i2}.') ExitLevel=int32(1) ExitText=string('Result of addition is {returnvalue}.') }
       .maxstack 2
       .locals init (
           [0] int32 CS$1$0000)
       L_0000: nop
       L_0001: ldarg.0
       L_0002: ldarg.1
       L_0003: add
       L_0004: stloc.0
       L_0005: br.s L_0007
       L_0007: ldloc.0
       L_0008: ret
   }

   .method public hidebysig static void Main(string[] args) cil managed
   {
       .entrypoint
       .maxstack 2
       .locals init (
           [0] class [log4net]log4net.ILog log,
           [1] int32 i1,
           [2] int32 i2,
           [3] int32 sum)
       L_0000: nop
       L_0001: call void [log4net]log4net.Config.XmlConfigurator::Configure()
       L_0006: nop
       L_0007: ldtoken Log4PostSharpDemo.Program
       L_000c: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
       L_0011: call class [log4net]log4net.ILog [log4net]log4net.LogManager::GetLogger(class [mscorlib]System.Type)
       L_0016: stloc.0
       L_0017: ldloc.0
       L_0018: ldstr "test"
       L_001d: callvirt instance void [log4net]log4net.ILog::Debug(object)
       L_0022: nop
       L_0023: call string [mscorlib]System.Console::ReadLine()
       L_0028: pop
       L_0029: call int32 Log4PostSharpDemo.Program::ReadNumber()
       L_002e: stloc.1
       L_002f: call int32 Log4PostSharpDemo.Program::ReadNumber()
       L_0034: stloc.2
       L_0035: ldloc.1
       L_0036: ldloc.2
       L_0037: call int32 Log4PostSharpDemo.Program::Add(int32, int32)
       L_003c: stloc.3
       L_003d: ldstr "Result is {0}."
       L_0042: ldloc.3
       L_0043: box int32
       L_0048: call void [mscorlib]System.Console::WriteLine(string, object)
       L_004d: nop
       L_004e: ret
   }

   .method private hidebysig static int32 ReadNumber() cil managed
   {
       .custom instance void [Log4PostSharp]Log4PostSharp.LogAttribute::.ctor() = { EntryLevel=int32(1) ExitLevel=int32(1) ExceptionLevel=int32(5) }
       .maxstack 2
       .locals init (
           [0] string line,
           [1] int32 CS$1$0000)
       L_0000: nop
       L_0001: ldstr "Please enter a number: "
       L_0006: call void [mscorlib]System.Console::Write(string)
       L_000b: nop
       L_000c: call string [mscorlib]System.Console::ReadLine()
       L_0011: stloc.0
       L_0012: ldloc.0
       L_0013: call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_CurrentCulture()
       L_0018: call int32 [mscorlib]System.Int32::Parse(string, class [mscorlib]System.IFormatProvider)
       L_001d: stloc.1
       L_001e: br.s L_0020
       L_0020: ldloc.1
       L_0021: ret
   }

}


If the custom code like the below is added to main function, it logs fine for log4net code and nothing from Log4PostSharp comes!

           log4net.Config.XmlConfigurator.Configure();
           log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
           log.Debug("test");


So I also suppose that configuration is fine but Log4PostSharp is not loading the configuration.
We are planning to use the Log4PostSharp in our project.
So its bit urgent.

Thanks in advance.
Post #3325
Posted 8/31/2008 8:00:58 PM


Michal Dabrowski

Community Member
Hi,

looking at the IL you posted, the logging code did not get injected and the Log attributes were not removed. It seems that the PostSharp has not been executed during the compilation. Do you have PostSharp installed in your system (I mean the full installation that integrates with the MSBuild process)? If yes, does the build output contain lines like the ones below?


...
Compile complete -- 0 errors, 0 warnings
...
PostSharp 1.0 [1.0.10.403] - Copyright (c) Gael Fraiteur, 2005-2008.
...
Post #3326
Posted 9/1/2008 6:44:18 AM


Community Member
Hi

Thanks the problem is solved.
I uninstalled my PostSharp & installed it again with Global configuration for MS Setup Build.
I was initially reluctant to install with Global build as i didnt want to associate PostSharp with all my existing projects which are not using PostSharp.

Is there any way I can use Log4Postsharp in my specific project without installing the Post Sharp with Global configuration for MS Setp Build.

Thanks once again.
Post #3327
Posted 9/1/2008 7:14:05 AM


Michal Dabrowski

Community Member
Hi,

sure there is. PostSharp chm documentation contains chapter: "PostSharp 1.0 / User Guide / Using the PostSharp Platform / Enabling or Disabling PostSharp". It describes how to configure your VS projects to use PostSharp without installing it globally (the "Enabling PostSharp On A Per-Project Basis" section).

With this setup, you may also want to consider downloading the "Binary - No Installer" package from http://www.postsharp.org/download/1.0/, adding the PostSharp binaries to your source code tree and referring it from there.

As a side note, this is how I enable PostSharp in my other projects.
Post #3328
Posted 9/9/2008 5:24:54 AM


Community Member
Hi,

I'm facing problem in logging from the application which is installed through an .msi file. When I build the application through VS.NET and then run it, it works perfectly well. But when I create the setup (.msi) file of my project, and then install it on my default website, it doesnot work.

I've done following settings in AssemblyInfo.cs files:

[assembly: XmlConfigurator(Watch = true)]

[assembly: Log(AttributeTargetTypes = "*", EntryLevel = LogLevel.Debug, ExitLevel = LogLevel.Debug, ExceptionLevel = LogLevel.Error)]

And my web.config and app.config file settings looks like:

<log4net>
<appender name="MainAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="QMSLog.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="100KB" />
<param name="RollingStyle" value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MainAppender" />
</root>
</log4net>

Please tell me where's the problem? Is it in the way I'm coding or something else?
Post #3329
Posted 9/9/2008 9:48:12 AM


Michal Dabrowski

Community Member
Hi,

your configuration and attributes seem to be correct. Could you confirm with the Reflector that the DLL files you distribute are woven correctly (i.e. contain the logging code)?

Can you also check if you are able to log events "manually", i.e. by instantiating a logger yourself in one of your methods and using it?
Post #3330
Posted 9/9/2008 10:03:47 AM


Community Member
Hi,

I've tested in the reflector. The code is getting injected but still I'm not getting the log file when the application is installed through .msi file.

As I told you it perfectly works when I run the application through VS.NET 2008 and according to my settings RollingFileAppender logic also works fine. Cant understand the problem with .msi file.
Has it anything to do with .NET 3.5?

Thanks
Ankit
Post #3331
Posted 9/9/2008 10:41:58 AM


Michal Dabrowski

Community Member
One of my applications also uses Log4PostSharp, runs under .NET 3.5 and is packaged as MSI installer. Everything runs fine there - so at least your problem is not caused by the framework version or packaging method.

Did you check if you are able to log anything "manually"? Just put the following lines into one of your methods:
var log = LogManager.GetLogger(typeof (object));
log.Info("Test.");
and make sure the method gets executed. Does the "Test." message get logged then? If you could check this, it would be very helpful for me.

I think that the problem may be related to log4net initialization. Log4PostSharp caches some values from log4net configuration and in some scenarios this caching may occur before the configuration is loaded (so that default, uninitialized values get cached). I plan to solve this issue in the next few days.
Post #3332
Posted 9/9/2008 4:35:26 PM


Community Member
Hi,

I've injected the piece of code that you provided but still no log file was created. According to my understand, since i haven't provided the path of the file it should be created in the bin folder, but its not there.

Any feedback on this situation?

Thanks
Ankit
Post #3333
Posted 9/10/2008 7:41:36 AM


Community Member
Hi,

I've again tried to do it manually. My only problem is the log file (QMSLog.txt in my case) is not getting created when I run my application through setup(.msi file). I've seen  in ILDASM, that the code is getting injected in the file.

Just need the log file.

Thanks
Ankit
Post #3334
Posted 9/12/2008 7:22:24 AM


Community Member
Hi Michal,

Can you please suggest some workaround so that we can start logging through our msi file? As we need to deploy our application very soon, we need to sort out this problem very quickly.

Thanks
Ankit
Post #3335
Posted 9/12/2008 9:54:32 AM


Michal Dabrowski

Community Member
Hi,

I've again tried to do it manually. My only problem is the log file (QMSLog.txt in my case) is not getting created when I run my application through setup(.msi file)


If you can't get logging to work even if you log manually, then I think the issue is unrelated to Log4PostSharp. I would suggest you checking you that:
[list][*]the assembly containing the method that does the first logging is decorated with the XmlConfigurator attribute,[/*:m]
[*]your application has enough security permissions to write to the directory where you want to store the log (I guess this depends on how IIS is configured).[/*:m][/list:u]
Post #3336
Posted 12/10/2008 8:14:04 AM


Community Member
Hi,
In the code below, txtWriter is null so thats why I'm not able to log through Postsharp. Any comments on this?

log4net.Util.CountingQuietTextWriter txtWriter = (log4net.Util.CountingQuietTextWriter)((log4net.Appender.TextWriterAppender)(((log4net.Repository.Hierarchy.RootLogger)((log4net.Repository.Hierarchy.Logger)(((log4net.Core.LoggerWrapperImpl)(logger)).Logger)).Parent).Appenders[0])).Writer;

Thanks
Ankit
Post #3337
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 9:44pm

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.223. 12 queries. Compression Disabled.