UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> App.config File Dynamically Set Trace Listner File Date    
 
   
Swatz
post Apr 3 2013, 03:35 PM
Post#1


UtterAccess VIP
Posts: 1,330
Joined: 26-November 02
From: Manitowoc, WI


I have the following setting up a trace source and 2 listners, this code works fine:
!--c1-->
CODE
<system.diagnostics>
    <sources>
      <source
       name="TestProgramTrace"
       switchName="SourceSwitch"
       switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <add
            name="EventLog"
            type="System.Diagnostics.EventLogTraceListener"
               initializeData="TestProgram">
            <filter type="System.Diagnostics.EventTypeFilter"
                    initializeData="Critical"></filter>                          
          </add>
          <add
            name="TextWriter"
            type="System.Diagnostics.TextWriterTraceListener"
               initializeData="I:\Logfile\LogFile.log"
            traceOutputOptions="DateTime">
            <filter type="System.Diagnostics.EventTypeFilter"
                    initializeData="Information"></filter>
          </add>
        </listeners>
      </source>
    </sources>
    <switches>
      <!-- You can set the level at which tracing is to occur -->
      <add name="SourceSwitch" value="All" />
      <!-- You can turn tracing off -->
      <!--add name="SourceSwitch" value="Off" -->
    </switches>
  </system.diagnostics>

This piece of the code I want to dynamically add the current date and time to the file and have tried the following without any success:
CODE
type="System.Diagnostics.TextWriterTraceListener"
               initializeData="I:\Logfile\LogFile.log"
type="System.Diagnostics.TextWriterTraceListener"
               initializeData="I:\Logfile\LogFile_{0:MMddyyyy}.log"

I have been hunting on the internet for the last 4 hours for a solution, have not found one.
I have seen the listeners in vb which handle file logging automically ... I just don't want to use that type of listener.
Any help would be greatly appreciated.
Thanks.
Frank
Go to the top of the page
 
Swatz
post Apr 5 2013, 12:44 PM
Post#2


UtterAccess VIP
Posts: 1,330
Joined: 26-November 02
From: Manitowoc, WI


To update the initializeData on the listener, it will need to be done from within the C# application and then refreshing the config file.
I will post the code once I have it figured out.
Thanks.
Frank
Go to the top of the page
 
Swatz
post Apr 6 2013, 12:44 PM
Post#3


UtterAccess VIP
Posts: 1,330
Joined: 26-November 02
From: Manitowoc, WI


Here is the solution:
!--c1-->
CODE
XDocument _XMLDocument;
            Configuration Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            _XMLDocument = XDocument.Load(Config.FilePath);
            
            // Retrieve values from a specific node and it's elements.
            var NodeData = from Nodes in _XMLDocument.Descendants("listeners").Elements().Attributes()
                           select Nodes;
            // Set logfile name
            foreach (var AttributeData in NodeData)
            {  
                if (AttributeData.Value == "%PathAndFileName%")
                {
                  AttributeData.Value = @"I:\LogFile\LogFile_04062013.log";
                }                                
            }
            _XMLDocument.Save(Config.FilePath);
            ConfigurationManager.RefreshSection("system.diagnostics");
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    13th December 2017 - 09:55 AM