How to get Tracelistnername from app.config - c#

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="mylistener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\application.txt" />
<add name="mylistener2" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\application2.txt" />
<remove name="mylistener"/>
<remove name="mylistener2"/>
</listeners>
</trace>
I have the above app.config file.I want to get name from <remove> tag.Can somebody help me to get the remove tag name from codebehind.

You can access the name of the current listeners using the following snippet:
foreach (TraceListener listener in System.Diagnostics.Trace.Listeners)
{
Console.WriteLine(listener.Name);
}
But there is no way to get the names of listeners removed immediately. Your app.config registers two listeners and immediately removes it, what doesn't make sense (to me).

Related

Write Trace Logs to text file in ASP.NET Core

I have an application written in .NetFramework 4.5 and the trace logs are added in the simplest possible way:
System.Diagnostics.Trace.WriteLine("Method in: ");
//
//
//
System.Diagnostics.Trace.WriteLine("Method out: ");
We use to write the trace logs into a text file using the <configuration /> tag in web.config.
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="app-logger" type="System.Diagnostics.TextWriterTraceListener"
initializeData="logfile.txt" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
I'm trying to implement the same functionality in .NetCore 3.1 but where should I place the <configuration /> tag?
You can use Serilog for logging into Text file.
https://nblumhardt.com/2016/10/aspnet-core-file-logger/

System.Diagnostics.Trace in WCF service implementation hosted on IIS will not work

I want to rant and rave, really, really bad because this just seems completely unbelievable. Instead, I'm going to try and describe my situation as clearly as possible and hope that someone will tap me on the shoulder and point out my obvious error and let me carry on merrily.
I have a client/server application where the server is a set of IIS hosted WCF services. Thus, there are contracts and I've got service code which implements them. When I publish Now, I'm having a problem with a duplex contract over a nettcp binding which does not stay alive after the first response even though the client channel is cached on the server for later use in a static list, is never closed, has timeouts set appropriately, etc but the bugger doesn't want to work.
So I figured I'd add some traces to write out some extra information about where in my service implementation code it could be having a problem. This is in addition to the System.ServiceModel trace source I have set up which logs all of the service model stuff and thankfully has been working without a problem for a while now. Below are two different versions of the relevant section of my web.config which I have tried in an attempt to get regular tracing to write to a file:
1:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
</trace>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
<!--use switchValue="All" or "Verbose" for more information-->
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="DuplexSource" switchValue="All" propagateActivity="true">
<listeners>
<add name="txt" />
</listeners>
</source>
</sources>
<sharedListeners>
<remove name="Default" />
<add name="txt" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\Logs\XXXErrorLog.txt" traceOutputOptions="Callstack" />
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\Logs\ServerTraces.svclog" />
</sharedListeners>
When I have it set thusly, I create a TraceSource object in my service code, TraceSource duplexSource = new TraceSource("DuplexSource");, then I use that trace source to write out statements. The file is never created, it is never attempted to be created (using Process Monitor I can see that it never attempts). The file permissions are wide open and the other log file for the xml listener works wonderfully.
2, I've also tried a more straight forward Trace configuration:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="txt" />
</listeners>
</trace>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
<!--use switchValue="All" or "Verbose" for more information-->
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<sharedListeners>
<remove name="Default" />
<add name="txt" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\Logs\XXXErrorLog.txt" traceOutputOptions="Callstack" />
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\Logs\ServerTraces.svclog" />
</sharedListeners>
</system.diagnostics>
Again, the log file is never even attempted to be created. Now, I've checked at runtime that I can write a file to that directory by actually doing so, then I wrote to that test file the contents of the Trace.Listeners collection to verify my listener was there, which it is. I even then called directy into the Listener's write method and this actually does get the file to be created and the WriteLine call successfully writes to the file as expected. Using System.Diagnostic.Trace.WriteLine does nothing however.
Any ideas of what could be causing this?

Loading configuration for System.Diagnostics.TraceSource from an xml file

In log4net, it is possible to choose between loading the configuration from the app.config, or from an arbitrary xml file.
Is it possible to load the configuration for System.Diagnostics.TraceSource from an arbitrary xml file?
System.Diagnostics classes look only at application configuration file. E.g. remarks section of SourceSwitch says:
To configure a SourceSwitch, edit the configuration file that
corresponds to the name of your application.
If you will look into code, you'll see that internally these classes use static DiagnosticConfiguration class which simply gets system.diagonostics configuration section from current app.config
BUT you can move system.diagonostics configuratin section to separate xml file. Just specify name of file where section will be defined:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics configSource="diagnostics.xml"/>
</configuration>
diagnostics.xml
<system.diagnostics>
<sources>
<source name="foo" switchName="bar"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"/>
</listeners>
</source>
</sources>
<switches>
<add name="bar" value="Warning"/>
</switches>
<sharedListeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console"/>
</listeners>
</trace>
</system.diagnostics>

Auto-load trace listener to all new trace sources

I am converting some code to use Microsoft tracing. What I'd like is to define all the listeners in one project and then use them from other assemblies, without having to explicitly load them there.
To clarify, this is what I'm doing now:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="myListener" type="ConsoleApplication4.LogListener, ConsoleApplication4"/>
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
And in the C# code:
var b = Trace.Listeners;
TraceSource tr = new TraceSource("Blah", SourceLevels.All);
tr.Listeners.Add(b["myListener"]);
tr.TraceEvent(TraceEventType.Warning, 5, "Hello");
What I would like is for myListener to be automatically added to any new trace source I create without having to look it up the way I'm doing now. Is this possible?
Define the trace source along with its listeners in config:
<system.diagnostics>
<sources>
<source name="Blah" switchValue="Warning">
<listeners>
<add name="myListener" />
</listeners>
</source>
</sources>
<!-- Note these are in sharedListeners rather than trace -->
<sharedListeners>
<add name="myListener" ... />
</sharedListeners>
<!-- Autoflush still works as expected -->
<trace autoflush="true" />
</system.diagnostics>
Then construct the TraceSource in code the way you already are (its trace level will be overridden by the switchValue in config), don't add any listeners to it and log to it as normal.

C# Add a footer to a trace listener when closed

How can one add a footer to a trace listener which is defined in the app.config:
<system.diagnostics>
<switches>
<!-- Set loglevel for diagnostic messages
(0=none, 1=errors, 2=warnings, 3=info, 4=verbose) -->
<add name="logLevel" value="4" />
</switches>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="FileListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="Logs\QFXLog.txt" />
<remove name="Default" />
</listeners>
</trace>
I want to write an end footer when this listener is closed.
What entries are to be defined in the config(if any?) and where must one define the footer string in code?
Thanks,
Juergen
I don't know of any way to handle this directly in the app.config file, but you could implement a class which inherits TextWriterTraceListener and then override its Close method:
namespace MyNamespace
{
public class FormattedTextTracer : TextWriterTraceListener
{
public override void Close()
{
// Write footer
Writer.WriteLine("==== Footer ====");
Writer.Flush();
base.Close();
}
}
}
And in the app.config file, replace the listener type with your class:
<listeners>
<add name="FileListener"
type="MyNamespace.FormattedTextTracer, MyNamespace"
initializeData="Logs\QFXLog.txt" />
<remove name="Default" />
</listeners>

Categories

Resources