none
how to manage application configuration? RRS feed

  • Question

  • Hi,

    Traces (for e.g.) are configured via the application configuration in a normal .net application (web/desktop). But in a VSTO addin we do not have access to the configuration file. So what is the recommended way to manage it?

    The issue I face is as follows:
    Say in a normal .net app I want to add tracing, I'd normally manually add the trace calls at critical points in the code, and in its configuration, I'll mention the "listeners" it should log to and the "switch level". So a typical config file would look like this:

      <system.diagnostics>
        <sources>
          <source name="ThreadSource" switchName="foo" switchType="System.Diagnostics.SourceSwitch">
            <listeners>
              <add name="console" type="System.Diagnostics.ConsoleTraceListener"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="foo" value="All"/>
        </switches>
      </system.diagnostics>

    And in code I'd have calls like:

    TraceSource ts = new TraceSource("ThreadSource");
    ts.TraceInformation("Thread Begins");
    //...

    Now in a vsto project even if I add an app.config the config settings don't associate with the plugin. Because the plugin is a dll. So given this challenge I know it means we've to write our own configuration loader, but how do I start with it? Or is there some feature in the .net framework that addresses this?


    I am a bundle of mistakes intertwined together with good intentions

    Tuesday, November 3, 2015 5:20 AM

Answers

  • Hi deostroll,

    >>Now in a vsto project even if I add an app.config the config settings don't associate with the plugin. Because the plugin is a dll. <<

    As far as I test, the config also works for the Office VSTO solution. Here is the test steps for your reference:
    1. Create an Word application-level add-in

    2. Add app configuration like below(app.config):

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.diagnostics>
        <sources>
          <source name="ThreadSource"
                  switchName="foo"
                  switchType="System.Diagnostics.SourceSwitch">
            <listeners>
              <add name="console"
                   type="System.Diagnostics.ConsoleTraceListener"/>
              <add name="text"
                   type="System.Diagnostics.TextWriterTraceListener"
                   initializeData="TextWriterOutput.log"></add>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="foo"
               value="All"/>
        </switches>
      </system.diagnostics>
    </configuration>

    3. Write a class to using TraceSource:

      class DiagnosticsTest
        {
            public void Main()
            {
                TraceSource ts = new TraceSource("ThreadSource");
                ts.TraceInformation(String.Format("{0}:Thread Begins",DateTime.Now));
                
                ts.Flush();
               
            }
        }


    4. Then add code below to call the trace code in the add-in startup event:

      private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
    
                new DiagnosticsTest().Main();
    }

    After running this sample, you can open the TextWriterOutput.log file to see the output.

    In addition, I suggest that you use other kind of listener since there is no console window in the VSTO solution.

    You can get more detail about listeners in System.Diagnostics from link below:
    System.Diagnostics Namespace

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, November 4, 2015 2:35 AM
    Moderator