1

I have a windows service application which is basically a solution created with VS2015 and contains 2 projects. The first project is dealing with the service itself (start, stop, etc) while the second one is maintaining the process that will be executed due to launch of the service. I am using the log4net to log messages into a file. I wanted to created 2 separate log files for each project. This has been configured into the service project and is up and running.
while it is not working for the second project. So mainly my question is how to configure log4net to log into different files within one solution?

Peter
  • 27,590
  • 8
  • 64
  • 84
Claritta
  • 189
  • 1
  • 4
  • 17

3 Answers3

2

Configure log4net within the executing project only. If you want log messages from an specific assembly logged to a separate file make sure the root namespace for that assembly is unique and define two log4net appenders, each logging to a separate file and use logging filters to filter by namespace.

<!-- Will log only from the My.Process namespace -->
<appender name="ProcessLog" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="My.Process" />
        <acceptOnMatch value="true" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="process.log" />
    <!-- layout, etc -->
</appender>

<!-- will log everything except from the My.Process namespace -->
<appender name="ServiceLog" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="My.Process" />
        <acceptOnMatch value="false" />
    </filter>
    <file value="service.log" />
    <!-- layout, etc -->
</appender>

And don't forget to add a reference to both appenders in the root element.

Andy Lamb
  • 2,151
  • 20
  • 22
  • 1
    unfortunately that didn't work for me, both projects keep logging to the same file – Claritta Jan 05 '17 at 17:18
  • OP wanted to know how to log to different files depending on circumstances, see this trail for solution: https://stackoverflow.com/questions/45234344/log4net-logging-to-wrong-appender – PKCS12 Feb 18 '20 at 17:51
0

Assuming your first and second project have different root namespaces (e.g. Project1.Service and Project2.MaintainProcess), you can use 2 appenders and the logger hierarchy to split the log files by project:

<log4net>
  <appender name="Project1Logger" type="log4net.Appender.RollingFileAppender">
    <file value="Project1Logger.log" />
  </appender>
  <appender name="Project2Logger" type="log4net.Appender.RollingFileAppender">
    <file value="Project2Logger.log" />
  </appender>
  <root>
    <level value="ALL" />
  </root>
  <logger name="{Project1RootNamespaceHere}">
    <appender-ref ref="Project1Logger" />
  </logger>
  <logger name="{Project2RootNamespaceHere}">
    <appender-ref ref="Project2Logger" />
  </logger>
</log4net>

If you don't want to use separate appenders and logger hierarchy then see this answer for an alternative method using a custom pattern substitution to use a single appender. I'm not sure of the performance implications of that approach though.

Community
  • 1
  • 1
thudbutt
  • 1,481
  • 1
  • 19
  • 32
0

Complete configuration (with a MinimalLock file appender):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
    <log4net>
        <!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
        <appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
            <file value="Info.log" />
            <encoding value="utf-8" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="100MB" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
            </layout>
            <filter type="log4net.Filter.LevelRangeFilter">
                <levelMin value="DEBUG" />
                <levelMax value="WARN" />
            </filter>
        </appender>
        <appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
            <file value="Error.log" />
            <encoding value="utf-8" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="100MB" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
            </layout>
            <filter type="log4net.Filter.LevelRangeFilter">
                <levelMin value="ERROR" />
                <levelMax value="FATAL" />
            </filter>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingFileAppender_Info" />
            <appender-ref ref="RollingFileAppender_Error" />
        </root>
    </log4net>
</configuration>
Nikolai Koudelia
  • 2,494
  • 1
  • 26
  • 28