Wim Sturkenboom

Wim Sturkenboom

  • NA
  • 1.7k
  • 3.1k

how to get configured values from log4net.config / app.confi

Sep 12 2014 12:18 PM
Hi all,

I'm using log4net for logging of my web stuff. I use a remoting appender and I have a little console application that receives the messages and writes them to the log file. It's all working fine. I'm now in the process of converting the console application to a windows service and like to retrieve some settings from the the log4net configuration so when the service starts, I can write this info in the event log. What I like to get out are the name of the logfile as well as the remoting port. Any help is highly appreciated.

App.config

<?xml version="1.0" encoding="utf-8" ?>

<!--

 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.

-->
 
 
<configuration>
 
  <!-- Register a section handler for the log4net section -->
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <!--
        %ndc is only for backward compatibility; see http://logging.apache.org/log4net/release/manual/contexts.html
        %ndc always seems to be null
                <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
        -->
        <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) - %message%newline" />
      </layout>
    </appender>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">

      <file value="dsrp.log" />
      <!--
      this might be a typo in the original
      <sppendToFile value="true" />
      -->
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="FILE HEADER&#13;&#10;" />
        <footer value="FILE FOOTER&#13;&#10;" />
        <!--
        <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
        -->
        <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="dsrp.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <!--
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        -->
        <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <!--<appender-ref ref="ConsoleAppender" />-->
      <!--<appender-ref ref="LogFileAppender" />-->
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
  <system.runtime.remoting>
    <application name="Log4netRemotingServer">
      <!-- We need to define the remoting channels on which we will publish
           the remote logging sink. -->
      <channels>

        <channel displayName="Server Channel" ref="tcp server" port="8085" />
      </channels>
    </application>
  </system.runtime.remoting>
   
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>


I've tried a couple of things; for the filename, I have managed to get the appender name (using GetRepository() and getAppenders()) but got stuck after that. For the second one, I have had no success. For both I've also tried the ConfigurationManager without success.

I clearly don't know how this stuff works ;) I do not necessarily need a pre-chewed code (although it might make it easier for me to understand how this stuff works), just some pointers as on how to access those keys / values can do for now.

Thanks in advance.