01 logo

aspnet logging

Logging with TraceSource

By Ricky Tucker JrPublished 3 years ago 3 min read
Like
aspnet logging
Photo by Florian Olivo on Unsplash

Creating a website in asp.net is something that can be challenging but also rewarding. The one thing that will always come up is errors, weather this is a database , or a file IO. In hosting solutions this isn’t always a straightforward thing. There are of course solutions such as third party solutions, manual writing them to a file, etc. A solution I decided to try was implementing System.Diagnostics tracing using TraceSource.

Using .nets built in Diagnostics TraceSource solution which is the 2.0 version of their solutions (Gewarren). TraceSource isn’t the only part of the solution, the user has the option of picking several TraceListeners that can be found at source .

  • TraceSource has several methods that are useful:
  • TraceEvent(TraceEventType, int identifier, and string with message) - writes an event.
  • TraceData(TraceEventType, id, data) - Write a object to file , This is good for writing exception data to file
  • TraceInformation(string) - Used for informational purposes.

Each one of these have a limitation. If called in a Finalization it can cause a ObjectDispositionException (TraceSource Class (System.Diagnostics)).

Two of the previously mentioned functions take an enum TraceEventType to specify what level of event it is. A description of some of the potential values are Critical for fatal error, Error for recoverable error, Information , Verbose, warning for noncritical (TraceSource Class (System.Diagnostics)).

A note on the Microsoft documentation before we review what it says.

The document for the listeners can be a little over simplified. To get them to work with the TraceSource class you need to add several things to the web config. Those are as follows.

<sources>

<source name="name" switchName="SourceSwitch"

switchType="System.Diagnostics.SourceSwitch" >

<listeners>

<add name="console" />

<remove name ="Default" />

</listeners>

</source>

</sources>

The source name has to match the name used on new TraceSource(“name”);

Then you need the switches.

<switches>

<add name="SourceSwitch" value="All" />

</switches>

This needs to match the sourceSwitch from the last section. The value on the switch can be from any from source.

The last section is sharedList and listeners

<sharedListeners>

<add name="console"

type="System.Diagnostics.TextWriterTraceListener"

initializeData="Devnightmare.log"/>

</sharedListeners>

<trace autoflush="true" indentsize="4">

<listeners>

<add name="console" />

</listeners>

</trace>

Many examples you find just show the portion about

<trace autoflush="true" indentsize="4">

<listeners>

<add name="console" />

</listeners>

</trace>

The next section will cover the details on the useful listeners for asp.net

Some general information include:

If file is in us or unavailable will each listener will prefix a GUID

To enable it need to either place

#define TRACE on each file using functionality

Or

/d:TRACE compiler command to enable tracer

The TraceListeners can either be included by config file or created programmatically and added at run time.

Being created at runtime is an easy way to introduce exceptions when adding them to the source.

This information is defined and applicable to each of the listeners and Trace functions

For the use case the first applicable Listener is TextWriter.

This will receive the TraceSource and output it to the designated file under tag InitializeData

An example configuration include is: but needs the additional information from above.

Config information

<configuration>

<system.diagnostics>

<trace autoflush="false" indentsize="4">

<listeners>

<add name="myListener"

type="System.Diagnostics.TextWriterTraceListener"

initializeData="TextWriterOutput.log" />

<remove name="Default" />

</listeners>

</trace>

</system.diagnostics>

</configuration>

Microsoft has a good reference below under reference:(TextWriterTraceListener Class (System.Diagnostics))

The next useful Listener is DelimitedListTraceListener:

This is very similar to the TextWriter except information will have a delimiter that is specified in the config or Listener creation process. The functions Write and WriteLine do not put the delimiter((DelimitedListTraceListener Class (System.Diagnostics)).

This can be useful because the delimiter can be ‘,’ and then that makes it easy to analyze in excel.

An example configuration is follows: but needs the additional information from above.

<configuration>

<system.diagnostics>

<trace autoflush="false" indentsize="4">

<listeners>

<add name="delimitedListener"

type="System.Diagnostics.DelimitedListTraceListener"

delimiter=","

initializeData="delimitedOutput.csv"

traceOutputOptions="ProcessId, DateTime" />

</listeners>

</trace>

</system.diagnostics>

</configuration>

The DelimitedListTrace can be referenced under the source (DelimitedListTraceListener Class (System.Diagnostics)).

If you found this helpful or want to follow my future developments follow me on https://www.buymeacoffee.com/rickytuckerjr or twitter.com/twinnightmare

Sources.

TextWriterTraceListener Class (System.Diagnostics). (n.d.). Retrieved January 10, 2021, from https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.textwritertracelistener?view=net-5.0

DelimitedListTraceListener Class (System.Diagnostics). (n.d.). Retrieved January 10, 2021, from https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.delimitedlisttracelistener?view=net-5.0

Gewarren. (n.d.). How to: Use TraceSource and Filters with Trace Listeners. Retrieved January 10, 2021, from https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-use-tracesource-and-filters-with-trace-listeners

TraceSource Class (System.Diagnostics). (n.d.). Retrieved January 10, 2021, from https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.tracesource?view=net-5.0

SourceLevels Class (System.Diagnostics). (n.d.). Retrieved January 10, 2021, from https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.sourcelevels?view=net-5.0

how to
Like

About the Creator

Ricky Tucker Jr

I will be feeling my stories with amazing adventures fueled by all the intangible ideas that float through my mind.

You can leave comments, help shape my adventures or buy me a coffee at https://www.buymeacoffee.com/rickytuckerjr

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.