4

I am using Logging Application block with C#.Net 2.0. My code is logging the error information to a flat file. I have set all required configuration in web.config like listeners, formatters and categories, etc as described in msdn and it is working fine. But the problem is, I cannot put more than 50 characters in le.Message property. In my case, the stack trace is more than 500 charactors long which I want to log into the flat file when error occurs.

Is there any limit on number of charactors we can put inside Message Property of LogEntry object? or is there any other way to log the stack trace into logger flat file?

Here is the simple code.

LogEntry le = new LogEntry();
le.Categories.Add("ErrorsToEventLog");
le.Categories.Add("ErrorsToLogFile");
le.Title = "Error message";
le.TimeStamp = System.DateTime.Now;
le.Severity = System.Diagnostics.TraceEventType.Error;
le.Message = "<text of error's stack trace>";
Logger.write(le);

configuration settings

<configSections>
 <section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=null" />

<section name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=null" />
</configSections>

Here is the formatter I used,

<formatters>
<add template="Timestamp: {timestamp} Message: {message}" 
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, 
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=null" name="Text Formatter" />
</formatters>

And here is the listener,

<add fileName="Logs/ErrorLog_{Date}.log" 
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.
CustomTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, 
PublicKeyToken=null" traceOutputOptions="None"
type="EnterpriseLibrary.Logging.Extensions.RollingFlatFileTraceListener,
EnterpriseLibrary.Logging.Extensions, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" name="Custom TraceListener" initializeData="" />

Categories

<categorySources>
<add switchValue="All" name="ErrorsToEventLog">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
<add switchValue="All" name="ErrorsToLogFile">
<listeners>
    <add name="Custom TraceListener" />
</listeners>
</add>
</categorySources>
MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Anil Soman
  • 2,443
  • 7
  • 40
  • 64
  • Can you post the listeners and sources configs? – StingyJack Aug 31 '10 at 11:51
  • edited my post with more info – Anil Soman Aug 31 '10 at 12:04
  • Is it possible that some special charactor in stack trace is blocking the file write process? because if I have more that 50 charactors in stack trace, the file itself is not created? This is just a guess... – Anil Soman Aug 31 '10 at 12:11
  • 1
    Stop guessing and figure it out. Try logging some hardcoded strings, then look at what's in your stack trace and see what the first non-logged character is. Note that changing configurations of the windows event log may require a reboot to pick up the changes (per the docs). – Greg D Aug 31 '10 at 12:25
  • 1
    Agree with Greg. Another suggestion will be to remove event log listener and try only with file listener. – VinayC Aug 31 '10 at 12:48

2 Answers2

1

To my knowledge, there is no such limit for log message. How do you set the stack trace to the message?

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • string str = "Message: " + exp.Message + Environment.NewLine + "Stack Trace: " + exp.StackTrace.ToString(); – Anil Soman Aug 31 '10 at 11:51
  • I re wrote all my code from scratch and found that it is running accepting more than 50 charactors. Don't know how it was failing previously!!! Need to check the stack trace at that time thoroughly.. – Anil Soman Sep 01 '10 at 06:13
0

Assuming your analysis is correct (it isn't convenient for me to double-check right now), have you considered creating a subclass for LogEntry that doesn't have the limits that you're running up against?

Greg D
  • 43,259
  • 14
  • 84
  • 117
  • nothing like that.. LogEntry is a class from namespace Microsoft.Practices.EnterpriseLibrary.Logging – Anil Soman Aug 31 '10 at 12:03
  • If it isn't sealed you could still subclass it. Microsoft typically does a pretty good job designing their published classes appropriately for subclassing, as well. In fact, looking it up, they've already subclassed it a couple times: http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logentry%28v=pandp.50%29.aspx – Greg D Aug 31 '10 at 12:19
  • thanks for reply and useful info... somehow it has started working after rewriting all code again. don't know how come! need to check the earlier stack trace text thoroughly – Anil Soman Sep 01 '10 at 06:15