0

I have an application where I use TraceListener to collect data/debugging information while application executes. There are more than one listeners at the same time, one of them will write trace information to disk file, other may collect in string (for a short period of execution) and then it will be displayed to user or might saved to another file as per needs.

All went well, unless we decided something else.

We had lot of plugins which were loaded at runtime and executed. However, the issue with that was it was hard to replace the plugin DLL since it was used by application, so we had to stop and replace DLL and then start it again. We decided to use appDomain to load plugin into so that we can easily replace DLL without restarting the application(s). That was also went well, but one major issue came in. the trace listerns were blind. Since the plugins are loaded into different app domain, so the traces never reached to the parent app domain which was listening to it (the traces made by plugin, which is loaded into new appdomain).

Our trace listener looks like this.

Public Class StringTraceListener
    Inherits TextWriterTraceListener

    Dim sw As System.Text.StringBuilder = Nothing
    Public Overrides Sub WriteLine(ByVal message As String)
        Try
            MyBase.WriteLine(message)
            sw.AppendLine(Now.ToString & " : " & message)

        Catch ex As Exception
            'Do not write anything here... or it might go into recursive loop
        End Try

    End Sub

I wonder, if there is a way that we don’t have to make much changes and the trace listener might receive traces from plugins (child appDomain) as well? Any idea will be appreciated Thanks

Sameers Javed
  • 342
  • 2
  • 5
  • 16

1 Answers1

0

The config is read in when the application is loaded. So for a plug in where the assembly isn't loaded in the usual way, it is plausible that the config isn't being read. You can configure the trace listeners in code, although it is obviously less flexible.

To configure a trace listener in source code, add a listener to the TraceSource's Listener collection. Make sure this happens once for the life of that trace listener, else you will get duplicate Listeners.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • I forgot to mention that I already do that way. Installing/Starting listening from code itself, not from config. So in code, I install it like StringTraceListener.AddTraceListener("ListenerID) and there is nothing in the config file. But the thing is, no matter if you are using code initiated trace listeners or from config file, the traces are not sent back from the new app domain. – Sameers Javed Dec 30 '14 at 12:57
  • Seems like when you access the static variable (via the trace listener, all trace listeners are held in a static collection, 1 per domain) that you are either getting a copy, or getting the other app domains reference to the global trace listener collection, ref. http://stackoverflow.com/questions/9806372/static-variable-instances-and-appdomains-what-is-happening – MatthewMartin Dec 30 '14 at 13:58