简体   繁体   中英

How to enable WCF tracing programmatically?

I have a WCF service (project output type - windows application) hosted as Windows service. All client and server logic is in c# code and I haven't any configuration files. I need to enable wcf tracing without using config file. How can I do that?

You can programmatically configure what you would put in your .config file. When you initialize your service just add this code (customize it according to your logging requirements):

var listener = new XmlWriterTraceListener("Log.xml");
Debug.Listeners.Add(listener);

Note that you can set all properties (log level, for example) you need as you would do with attributes in your .config file:

Debug.AutoFlush = true;
listener.TraceOutputOptions = TraceOptions.Callstack;

You may also add your custom filters:

listener.Filter = new MyCustomTraceFilter();

By design you cannot do it see:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/25b17fef-5660-499d-ad1b-aaa3b8ab7f60/how-do-you-enable-wcf-tracing-without-using-a-config-file-programmatically?forum=wcf

You cannot do this without any config files. But you can have one system that Writes the config file for a second system. The first system would then start the second in a seperate app pool. The logging would be turned on in the second system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM