Problem writing to a flat file/rolling file using the Logging application block

Topics: Logging Application Block
Feb 21, 2011 at 11:46 PM

I have configured a Category "TraceCategory" to log the trace to a File named "Trace.log".

Using this Category in both my Business and Data Access layer has follows:

This is my Business layer

TraceManager traceManager = EnterpriseLibraryContainer.Current.GetInstance<TraceManager>();
            using (traceManager.StartTrace("TraceCategory"))
            {
               
                    DataAccesslayer.Client DalClient = new DataAccesslayer.Client();
                    return DalClient.GetClientbyId(CustomerID);
                
                }

This is my Data access layer

TraceManager traceManager = EnterpriseLibraryContainer.Current.GetInstance<TraceManager>();
            using (traceManager.StartTrace("TraceCategory"))
            {
               
                //doing some code here                 
                }

 

What iIexpected when I run my application was the logging block application write the trace to Trace.log file.But only the trace of Business layer is writen to Trace.log file

and the trace for Data access is writen to a different file of its own.Can you please help how i can i write to a single file.

Feb 22, 2011 at 2:25 AM

I tried to reproduce your scenario by calling a two separate class methods which is also decorated with tracing but unfortunately I was unable to get the result you mentioned. I'm thinking if your Rolling Flat File may have been set to a small roll size interval unintentionally that's  why it looks like it is logging in a 2 separate file. Could you check your Rolling Flat File Trace Listener configuration? Or if you can send to us a sample repro project for this that'll be also good for us to have a further investigation regarding this. Thanks.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 22, 2011 at 2:54 AM

This is my Configuration setting

 <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="C://temp/trace1.log" formatter="Text Formatter"
                rollFileExistsBehavior="Overwrite" rollInterval="Day" rollSizeKB="10000"
                maxArchivedFiles="5" traceOutputOptions="None" />

The methods are in two differnet dll's. Bal method calls to the method in Dal.IF the two methods are in same class file it works fine.But in
two different class library it writed into two different file.Thanks for your reply.
Feb 22, 2011 at 7:56 AM

Unfortunately, I still can't reproduce. I have put the two method separated in a two class library project wherein the first Project's class method has been called in my main project, while that First Project's class method called the Second Project's Class Method but the trace still logs in the same file and using the configuration you have sent.

Could you send a sample repro project for this for us to further investigate.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 31, 2011 at 1:23 PM

As requested i have mailed you the sample project for your investigation.

Thanks.

Apr 1, 2011 at 12:10 AM

Hi,

Please reply asap.


Thanks

Apr 1, 2011 at 12:41 AM

Hi Santosh, I'm currently looking into this.  I'll give you an update in a while.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 1, 2011 at 1:12 AM

I see, I think what happens is that the file is still locked by the first LogWriter object that was created inside the LogHandler class while the second and third attempt to log to the file is happening.  When a trace listener is unable to acquire a lock on a file, it creates a new file prepending the original file with a GUID.

My suggestion would be to not create a new instance of LogWriter inside the LogHandler class; instead, provide a single point of access for it.  You can either just resolve an instance of LogWriter from EnterpriseLibraryContainer.Current object inside the Logger method:

LogWriter defaultWriter = EntepriseLibraryContainer.Current.GetInstance<LogWriter>();

or create a class having a static property of type LogWriter

public class Common
{
     private static IUnityContainer unityContainer;
     public static IUnityContainer Container
     {
            get
            {
                if (unityContainer == null)
                {
                    unityContainer = new UnityContainer();
                   unityContainer.AddNewExtension();
                    
                }
                return unityContainer;
            }
     }

     public static LogWriter Writer
        {
            get
            {
                return Container.Resolve();
            }
        }
}

You'll then only need to use the Common.Writer object when performing logging.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 1, 2011 at 1:21 AM

Thanks for the reply.

Apr 1, 2011 at 4:42 AM

Hi

 

The first problem is solved.Now when i try to use ,  using (traceManager.StartTrace("TraceCategory")) to my methods to the same sample project i sent you

it is writing to multiple files. i have defined to categories 1) for debug purpose (solved)and the 2) for trace.

Can you please give me a solution ASAP.

 

Thanks.

 

Apr 1, 2011 at 5:51 AM

Where exactly did you put the traceManager.StartTrace call?

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 1, 2011 at 6:28 AM

I have Mailed you the sample code.Please check and let me know the solution ASAP.

 

Thanks

Apr 1, 2011 at 6:51 AM

The easiest solution is to remove your instance of _defaultWriter in your LogHandler class.  Whenever you need a LogWriter object, just get that from the EnterpriseLibraryContainer.Current object.

LogWriter defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 1, 2011 at 7:01 AM

tried but no lcuk.

Apr 1, 2011 at 7:08 AM

Sorry.

It works fine now.

Thanks for your help.