1

I am trying to check whether the log is enabled or disabled based on the ini file. But i will have to check this condition every time whether it is enabled or disabled. I will have to check this log file is enabled or disabled once and then use it everywhere. How can I do this?

if ("true".Equals(m_objGlobalConfig.SoapLog))
   {
     log.ErrorFormat(se.Message + Environment.NewLine + Environment.NewLine, se.StackTrace);
   }

Is there anything I can do this customization for the appender ?

roopini n
  • 503
  • 2
  • 7
  • 29

1 Answers1

1

You could simply disable the log4net initialization based on this parameter, this would prevent log4net from being configured and logging:

During the initialization of your program just do :

if ("true".Equals(m_objGlobalConfig.SoapLog))
{
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("path/to/file"));
}

and then you can log normally

samy
  • 14,832
  • 2
  • 54
  • 82
  • so we can remove this configuration from the assemblyInfo.cs and put this in the config class? – roopini n Jun 26 '14 at 08:15
  • 1
    If that's where you feel it is adequate yes. The assemblyInfo attribute is one way to configure log4net. I personnaly don't like it but it's a valid an easy way to do it. – samy Jun 26 '14 at 09:27
  • If i remove the initialization of log4net from the assemblyInfo, it is not able to create the log file at all. – roopini n Jun 26 '14 at 09:39
  • I have done this if ("true".Equals(m_boolSoapLog)) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(m_strGeneralFilePath)); } and m_strGeneralFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Config.APPDATA_PATH; But then it is not able to create the file. And the configuration of my appconfig is http://stackoverflow.com/questions/24136199/how-to-create-a-file-in-the-appdata-folder-using-log4net – roopini n Jun 26 '14 at 09:42
  • `ConfigureAndWatch` takes the path to the configuration file, not the file you want to log to. The file you want to log to is defined in the configuration file – samy Jun 26 '14 at 09:48
  • I did this if("true".Equals(m_boolSoapLog)) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppConfigExePath)); } and tried to create string AppConfigExePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + APPDATA_PATH; where this points to the Excel.exe.config file. But it dint work again. – roopini n Jul 01 '14 at 04:41