1

I'm creating the enterprise logging setting dynamically.

I have added the ConfigurationManager.RefreshSection("loggingConfiguration") to refresh the app.config in my windows service after saving the config. But log is not creating first few seconds (10 sec). I Lost some data that need to log.

Below is code i written to create log setting programitically.

    public static void CreateLoggingSettings(string configpath, string logCategory, string logfilePath)
    {
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        TextFormatterData txtformat;
        FlatFileTraceListenerData tldata;
        LogSource ls;
        TraceSourceData tsd;
        TraceListenerReferenceData tlr;
        try
        {

            fileMap.ExeConfigFilename = configpath;
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            LoggingSettings loggingSettings = config.GetSection(LoggingSettings.SectionName) as LoggingSettings;

            if (loggingSettings == null ||
                (loggingSettings.Formatters.Contains(logCategory) &&
                 loggingSettings.TraceListeners.Contains(logCategory))) return;

            txtformat = new TextFormatterData(logCategory, "{timestamp(local)}{tab}{message}");
            loggingSettings.Formatters.Add(txtformat);

            tldata = new FlatFileTraceListenerData(logCategory, logfilePath + logCategory + ".log",
                txtformat.Name);
            loggingSettings.TraceListeners.Add(tldata);

            ls = new LogSource(tldata.Name, System.Diagnostics.SourceLevels.All);
            tsd = new TraceSourceData(ls.Name, System.Diagnostics.SourceLevels.All);
            tlr = new TraceListenerReferenceData(tldata.Name);
            tsd.TraceListeners.Add(tlr);

            loggingSettings.TraceSources.Add(tsd);
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("loggingConfiguration");

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            fileMap = null;
            txtformat = null;
            tldata = null;
            ls = null;
            tsd = null;
            tlr = null;
        }
    }
}

But it works fine. when i debugging the service. Any ideas?

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
vinay
  • 11
  • 1
  • Try to work with Config as with simple xml file. After modify that as xml file refresh that using ConfigurationManager. – Frank59 Aug 06 '13 at 06:28

0 Answers0