Writing to a new log file each day with TraceSource - c#

I am using a logger in my application to write to files. The source, switch and listeners have been defined in the app.config file as follows:
<system.diagnostics>
<sources>
<source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener.log" />
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Information" />
</switches>
</system.diagnostics>
Inside, my .cs code, I use the logger as follows:
private static TraceSource logger = new TraceSource("LoggerApp");
logger.TraceEvent(TraceEventType.Information, 1, "{0} : Started the application", DateTime.Now);
What would I have to do to create a new log file each day instead of writing to the same log file every time?

What would I have to do to create a new log file each day instead of writing to the same log file every time?
You'd have to make your own TraceListener instead of using TextWriterTraceListener. This would allow your TraceListener implementation to change log files daily, or do any other custom behavior you wish.

Try use:
<system.diagnostics>
<sources>
<source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener-{0:dd}-{0:MM}-{0:yyyy}.log" />
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Information" />
</switches>
</system.diagnostics>

Related

Configure a TraceSource with two TraceListeners and different filters in app.config

I am using the .net framework classes TraceSource and TraceListener for logging. They are configured in my app.config file. The same TraceSource may log to multiple different TraceListeners writing to different logfiles.
Now I want to filter TraceEvents from a specific TraceSource, but only for one TraceListener and not the other
In my app.config file I have some shared TraceListeners:
<sharedListeners>
<add name="log1" type="MyTraceListener,MyAssembly" initializeData="log1.log"/>
<add name="log2" type="MyTraceListener,MyAssembly" initializeData="log2.log"/>
<add name="logWarning" type="MyTraceListener,MyAssembly" initializeData="logWarning.log">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning"/>
</add>
</sharedListeners>
And I have configured some TraceSources
<source name="Foo" switchValue="All">
<listeners>
<add name="log1"></add>
<add name="log2"></add>
<add name="logWarning"></add>
</listeners>
</source>
<source name="Bar" switchValue="All">
<listeners>
<add name="log1"></add>
<add name="log2"></add>
<add name="logWarning"></add>
</listeners>
</source>
The Listener logWarning only receives TraceEvents with severity warning or above, from all sources. So far so good.
Now I want to configure the Listener log2 to receive only warnings from the source Foo, but still everything from source bar. The Listener log1 should also still receive everything from Fooand bar.
What is the corrrect app.config syntax for this?

How to show the detailed stack trace in IIS?

I have an application hosted in IIS, but a null reference exception occurs:
As you can see, there's no stack trace, only a general exception "object reference not set to an instance of an object" is shown
But I don't know where the exception flows.
I am unable to replace the whole directory to the server since there is some other folders there.
Is there any way to fix this?
If you do have access to the assemblies, then +72 is almost enough for you to know which line of code is the cause,
http://odetocode.com/blogs/scott/archive/2005/01/25/funny-numbers-in-my-stack-trace.aspx
Add this to your web.config.
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="CardSpace">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.IO.Log">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.Runtime.Serialization">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.IdentityModel">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\logs\Traces.svclog" />
</sharedListeners>
</system.diagnostics>
This outputs a more in depth trace log. You'll need service trace viewer tool to view it. https://msdn.microsoft.com/en-us/library/ms732023%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Since you accepted the answer that tells you to find the offending source code line I assume that's what you want.
Deploy the PDBs that the compiler generates to production. This has no performance impact and provides line numbers.

logging services calls in .txt file

I am trying to user the following code
private CModel[] getConfig(string CID, string Program)
{
ServiceManagement.ServiceClient obj;
List<ServiceManagement.ManagementApiRepositoryCConfig> executedService;
obj = new ServiceManagement.ServiceClient();
executedService = new List<SaServiceIdentityManagement.ManagementApiRepositoryCConfig>();
executedService = obj.getClubConfigSingle(CID, Program);
return executedService.Select(x => new CModel
{
CID = CID,
ProgramName = x.Name,
ProgramURL = x.Value,
}).ToArray();
}
and
using (StreamWriter w = File.AppendText("log.txt"))
{
Log("call 1", w);
Log("call 2", w);
}
What I am wanting to do is build a .txt, or xml, js/json file to log requests to the service
I am not sure why Im getting nothing added to the log.txt file
Thanks M
It sounds like you are trying to build something that already exists by default in the .net framework. I think what you're looking for is the tracing functionality that's in system.diagnostics. Try adding this to your config file:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information,ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\logs\TracingAndLogging-client.svclog" type="System.Diagnostics.XmlWriterTraceListener"
name="xml" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
It's easy and cheap to implement and there's even a special software that displays it neatly for you.
You can find more information about tracing here: https://msdn.microsoft.com/en-us/library/ms751526%28v=vs.110%29.aspx

Set logging level of autoflush .Net Trace Listener

In a .Net 4.0 web-service I am using trace autoflush to write to a log file.
By adding the following in the web.config:
<trace autoflush="true" >
<listeners>
<add name="TextWriter"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.log" >
</add>
</listeners>
</trace>
Am trying to find a way to log only Trace.TraceError("error info") and exclude Trace.TraceInformation("some verbose debugging stuff") without altering my code and just changing the web.config?
The information I have found on MSDN shows how this can be done by adding code that calls Trace.Flush() and adding sources, switches and sharedlisteners, however I would like to continue using auto-flush and not alter the code.
Many thanks in advance :)
Old answer:
This appears to be impossible. Trace auto-flush does not have the
capacity to have it's level set in the web.config.
Update:
The listener may have a filter applied as follows
<configuration>
<system.diagnostics>
<trace autoflush="true" >
<listeners>
<add name="TextWriter"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace4.log" >
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
</add>
</listeners>
</trace>
</system.diagnostics>
Note that the initializeData is a string and may take the values of Warning or Error. This filters the trace output accordingly.
Many thanks to Josip for following up on this question.
Maybe you can use "switchValue" for this purpose like this:
<system.diagnostics>
<trace autoflush="true">
</trace>
<sources>
<source name="SCInterface" switchType="System.Diagnostics.SourceSwitch" **switchValue**="All">
<listeners>
<remove name="default"/>
<add name="HSInterface"
type="XYZ.Diagnostics.CyclicTextTraceListener, XYZ.Base3x"
initializeData="D:\Trace\HSR.HSInterface.Trace.log, Size, 10, 5"
traceOutputOptions="DateTime,ThreadId,Callstack" />
</listeners>
</source>
</sources>
</system.diagnostics>
For switch value you would put Warning or Error...

Can I turn off tracing for a single assembly that my code references?

I have a certain framework of code, and I have a TraceListener defined for two reasons:
Back-compatibility with a lot of the old logging that was done via Trace.Write until we update it, and
It's nice to be able to instrument the other assemblies our code references if we need to.
However, I have one assembly (not ours) that logs a lot of pointless data that doesn't help us debug anything. How can I turn off tracing for this one assembly (or, alternately, the facade project we built around it), while leaving it on for the rest of the application?
I've tried various flavors of configuration in our facade project, usually looking like the following, to no avail. I've tried adding <remove> elements that match the <add> elements which setup the logging in the first place, tried <clear>ing them, setting <trace enabled="false"> and at least three other attempts. Thanks for any help you can provide!
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<clear/>
</listeners>
</trace>
<switches>
</switches>
</system.diagnostics>
You can write your own trace filter, for use with your TraceListener. Inside this filter you can look for your assembly in stackTrace and turn off event tracing.
In my case I wrote filter (see: DotNetOpenAuthFilter) based on EventTypeFilter, which filters events only from the DotNetOpenAuth library.
Then connect the filter to the listener in the web.config:
<configuration>
<system.diagnostics>
<trace>
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" >
<filter type="Common.Log.DotNetOpenAuthFilter, Common" initializeData="Warning" />
</add>
</listeners>
</trace>
</system.diagnostics>
</configuration>
Use TraceSource.
Initialize it in your trace source.
TraceSource logger = new TraceSource("Class1");
Call it from critical points in code:
logger.TraceInformation("Hello from Class1");
Be sure to edit your application configuration:
<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="Class1" switchName="Class1Switch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"></add>
<add name="csv" />
<!-- or you can add your own listener here -->
</listeners>
</source>
</sources>
<switches>
<add name="Class1Switch" value="Information" />
</switches>
<sharedListeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="csv" type="System.Diagnostics.DelimitedListTraceListener"
delimiter="|" initializeData="d:\data\tracing\trace.log"
traceOutputOptions="Timestamp, ThreadId, LogicalOperationStack, DateTime, ProcessId">
</add>
</sharedListeners>
</system.diagnostics>
If say, you want to only log errors, change the switch:
<add name="Class1Switch" value="Error" />
To switch it completely off:
<add name="Class1Switch" value="Off" />

Categories