1

Hi there i'm trying to write log for my live website and am having problems with my paths, I need to use the method Server.MapPath but i need it done in my web.config.

How would i go about this?

The file value holds the path, but i need to set a Server.MapPath on it.

  <log4net>

<add key="LogFilePath1" value="../Logs/CurrentLog" id="FP1"/>
<add key="LogFilePath2" value="../Logs/CurrentLog.txt" id="FP2" />

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="../Logs/CurrentLog"/> 
  <appendToFile value="true"/>

  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="10"/>
  <maximumFileSize value="10000"/>
  <staticLogFileName value="true"/>
  <filter type="log4net.Filter.LevelRangeFilter">
    <acceptOnMatch value="true"/>
    <levelMin value="INFO"/>
    <levelMax value="FATAL"/>
  </filter>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n"/>
  </layout>
</appender>

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <file value="../Logs/Log.txt"/>
  <appendToFile value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <header value="**"/>
    <footer value="**"/>
    <conversionPattern value="%newline%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

<root>
  <level value="DEBUG"/>
  <appender-ref ref="LogFileAppender"/>
  <appender-ref ref="ConsoleAppender"/>
  <appender-ref ref="RollingFileAppender"/>
</root>

<logger name="ConsoleApp.LoggingExample">
  <level value="ERROR"/>
  <appender-ref ref="EventLogAppender"/>
</logger>

Logger code C#:

public class Logger
    {
        protected static readonly ILog logger = LogManager.GetLogger(typeof(Logger));



        public void logError(string message, Exception ex)
        {
            HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath1"]));
            HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath2"]));
            log4net.Config.XmlConfigurator.Configure();
            logger.Error(message + ex);
        }


        public void logInfo(string message)
        {
            HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath1"]));
            HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath2"]));
            log4net.Config.XmlConfigurator.Configure();
            logger.Info(message);
        }

    }

I have added C# code to Server.MapPath the Keys, i'm just struggling calling the keys in thhe file value =

Peter
  • 27,590
  • 8
  • 64
  • 84
Pomster
  • 14,567
  • 55
  • 128
  • 204

2 Answers2

1

Store your path in the config as

<add key="LogFilePath1" value="~/Logs/CurrentLog" id="FP1"/>

From your code read the key

string path = GetPath("LogFilePath1");
string fullPath = System.Web.HttpContext.Server.MapPath(path);
codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • Thanks i just posed code of me doing something similar, how then i can set the file value = LogFilePath1? – Pomster Nov 19 '12 at 09:35
  • Sorry am not familiar with log4net. You can't use Server.MapPath in the config file – codingbiz Nov 19 '12 at 09:45
  • Yes i have switched it to do the Server.MapPath in C# code, But now i have with the correct address and i need to move this address to the value of How can i make the Value = the Key LogFilePath1 ?? – Pomster Nov 19 '12 at 10:03
0

Log4net does not use Server.Map path when reading the paths from your configuration files. It does not know it is running in a web context. I geuss you can implement your own version of the Rolling File Appender and override the function which gets the path from the config. However I would just use the full paths to the log locations. Normally you do want to log on an other disk your application is running to prevent running out of diskspace and killing your application.

Peter
  • 27,590
  • 8
  • 64
  • 84