Different Log file for different sources

Topics: Semantic Logging Application Block
Jul 3, 2013 at 3:41 PM
Hi,

I have a around 5 sources for whom logging is enabled. Assume that I don't want to use DB logging. I'm logging in flat text files. The problem is that logs of all sources are getting logged into a single file. Rolling flat file also doesn't solve the issue since each file has logs from difference sources.

Is there a way to enable separate log files for each source. This would avoid crowing of the log text file.

I tried creating multiple flat file sinks each having different source but the logging service doesn't allow duplicate sinks.

One way I can think of is to create custom flat file sinks, one each for every sources, but that sounds tedious.

Is there a cleaner way to achieve this?

Himanshu
Editor
Jul 10, 2013 at 5:25 AM
So you have around 5 different event sources. Are you trying to capture events using the out of process service? If you are using the out of process service you can define a separate sink for each event source:
<sinks>
  <rollingFlatFileSink name="RollingFlatFileSink"
    fileName="RollingFlatFile.log"
    timeStampPattern="yyyy"
    rollFileExistsBehavior="Overwrite"
    rollInterval="Day">
    <sources>
        <eventSource name="MyCompany-Shipping-DataAccess" level="Error"/>
    </sources>
  </rollingFlatFileSink>

  <rollingFlatFileSink name="RollingFlatFileSink"
    fileName="AnotherRollingFlatFile.log"
    timeStampPattern="yyyy"
    rollFileExistsBehavior="Overwrite"
    rollInterval="Day">
    <sources>
        <eventSource name="MyCompany-Inventory-UI" level="Error"/>
    </sources>
  </rollingFlatFileSink>
</sinks>
If you are running in-process you can set up multiple ObservableListeners for each source:
ObservableEventListener listener1 = new ObservableEventListener();
listener1.EnableEvents(MyFirstEventSource.Log, EventLevel.LogAlways, 
    MyFirstEventSource.Keywords.Perf | MyFirstEventSource.Keywords.Diagnostic);
listener1.LogToFlatFile(@"C:\Logs\MyFirstEventSource.log");

ObservableEventListener listener2 = new ObservableEventListener();
listener2.EnableEvents(MySecondEventSource.Log, EventLevel.LogAlways, Keywords.All);
listener2.LogToFlatFile(@"C:\Logs\MySecondEventSource.log");

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jul 10, 2013 at 7:13 AM
Hello Randy,

As I mentioned previously that creating multiple flat file or rolling file sinks in the same configuration file gives me an error.

Here is the detailed error:
One or more errors occurred when loading the TraceEventService configuration file.
Configuration file: C:\EnterpriseLibrary\SemanticLoggingService\SemanticLogging-svc.xml
There is a duplicate key sequence 'RollingFlatFileSink' for the 'http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw:sinks' key or unique identity constraint.
Line number: 33, Line position: 3


It doesn't seem that duplicate same sink entries are allowed. Is there any way to tweak it? Or is there any other way to log different log files for every source?

Thanks,
Himanshu
Editor
Jul 11, 2013 at 5:13 AM
Edited Jul 11, 2013 at 4:28 PM
That message just means that the "key" is not unique. The solution is to use a different "key" in the configuration. In this case the "key" is the name property of the sink. So change the name to be unique (e.g. one sink use MyEventSourceFlatFileSink and the other use AnotherEventSourceFlatFileSink or whatever makes sense to you). So the configuration should look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw SemanticLogging-svc.xsd">
  
  <!-- Optional settings for fine tuning performance and Trace Event Session identification-->
  <traceEventService/>

  <sinks>
    <!-- The service identity should have security permissions to access the resource according to each event sink -->
    <flatFileSink name="MyEventSourceFlatFileSink" fileName="MyEventSource.log" >
      <sources>
        <eventSource name="MyEventSource" level="Verbose"/>
      </sources>
      <eventTextFormatter header="----------"/>
    </flatFileSink>

    <flatFileSink name="AnotherEventSourceFlatFileSink" fileName="AnotherEventSource.log" >
      <sources>
        <eventSource name="AnotherEventSource" level="Verbose"/>
      </sources>
      <eventTextFormatter header="----------"/>
    </flatFileSink>

  </sinks>

</configuration>

With the above configuration in place there should be 2 separate log files created (provided that events are being sent by both EventSources).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/7/2013 at 11:11 PM
Jul 11, 2013 at 4:26 PM
Thank you so much Randy! My bad since I didn't try changing the name. This fixed the issue.

Thanks,
Himanshu