How to write to different log file for every project

Topics: Logging Application Block
Jun 7, 2012 at 5:23 AM

RupeshPrasad wrote:


I am working with Microsoft.Practices.EnterpriseLibrary.Logging,

In my solution i have 8 projects including one webservice, i have only one webconfig file in entire solution which resided under the webservice.

Based on certain condition webservice is calling any one of the project with in the solution. for this scenario i want the separate log file for each project lets say

when ProjectA is getting called then it should write in projectA.LOG and when projectB is getting called then it should write in projectB.log file and so on.

pls suggest how to implement it.



Jun 7, 2012 at 5:41 AM

Hi Rupesh,

Probably the easiest way to have each assembly (project) log to it's own file is by using categories.  So in each assembly you could retrieve the assembly name and use that as the log category.  Then all you would need to do to add categories to the web.config and associate that category to the appropriate flat file trace listener.

So a crude example would look something like this:

namespace ClassLibrary1
    public class Class1
        // Use AssemblyName as CategoryName
        public static string CategoryName = new System.IO.FileInfo(Assembly.GetExecutingAssembly().Location).Name;

        public static void Do()
            var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            logger.Write("Test", CategoryName);

Then the configuration would look something like this:

    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="ConsoleApplication1.exe">
          <add name="ClassLibrary1 Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              fileName="ClassLibrary1.dll.log" formatter="Text Formatter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
          <add switchValue="All" name="ClassLibrary1.dll">
              <add name="ClassLibrary1 Trace Listener" />

Randy Levy
Enterprise Library support engineer 

Jun 7, 2012 at 6:20 AM
Edited Jun 7, 2012 at 9:55 AM

hi Randy,

Thanks for your valuable reply.

I did as u have mentioned with little change, i have created multiple listeners and categorySources for different projects and using the name of listener as categoty parameter while logging and its working fine. i just need to pass the category name while writing.

Its working fine but one more problem i am facing pls help me in this regard :

Among all 8 projects there is one core project which is being used with rest of the 5 projects, basically the core has some common functionality which is being used by different projects on as and when required basis.

i want whenever my core project is getting called from any project it should always write in a single logfile i.e. corelogfile.log, but in my case it is not happening when projectA calling core project the core log are getting written in corelogfile.log i.e. (MailerCore.log) but when ProjectB is calling core project the core log are getting written in different log file with in the same folder basically it is adding a GUID before the corelogfile.log i.e. (b5b4ee06-1d67-427c-be39-97117d09481dMailerCore.log) and it is happening with every project.



Jun 7, 2012 at 6:45 PM

Multiple AppDomains (and by definitions processes) as well as multiple Trace Listeners with the same file name cannot log to the same file name.  

If you want multiple proccesses to log to one file then you could look at the Distributor Service.

Randy Levy
Enterprise Library support engineer