Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:53:10+00:00 2026-05-30T19:53:10+00:00

I’ve been trying to catch exceptions in my WCF service and store them in

  • 0

I’ve been trying to catch exceptions in my WCF service and store them in a log file (I don’t want the client to get any specific error message at this point).

Having read a few tutorials online I’ve added some logging configuration to the <system.diagnostics> section of web.config:

<sources>
  <source name="System.ServiceModel.MessageLogging" switchValue="Error">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="ServiceModelMessageLoggingListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.ServiceModel" switchValue="Error" propagateActivity="true">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="ServiceModelTraceListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>    
<sharedListeners>
  <add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
  <add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
</sharedListeners>

In the event of an exception I’ve been tried using Debug.WriteLine and Trace.WriteLine but nothing is getting written to the tracefile.

I know IIS / WCF can write to the log file, if I set the switchValue for either of the sources to be Verbose I get plenty of information (too much to be of use), but I only want the exception code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:53:12+00:00Added an answer on May 30, 2026 at 7:53 pm

    Try this,
    consider the following class

    public class TraceUtility 
    {
        private TraceSource trcSrc;
    
        public TraceUtility(string traceSourceName)
        {
            if (string.IsNullOrEmpty(traceSourceName))
            {
                throw new ArgumentNullException(traceSourceName);
            }
    
            trcSrc = new TraceSource(traceSourceName);
        }
    
        public void TraceError(int id, string message)
        {
            trcSrc.TraceEvent(TraceEventType.Error, id, message);
        }
    
        public void TraceError(int id, string message, params object[] args)
        {
            trcSrc.TraceEvent(TraceEventType.Error, id, message, args);
        }
    
        public void TraceError(string message)
        {
            TraceError(0, message);
        }
    
        public void TraceError(string message, params object[] args)
        {
            TraceError(0, message, args);
        }
    
    }
    

    Now add the following config section to your configuration file

    <system.diagnostics>
        <sources>
            <source name="xyz" switchName="AllSwitch">
                <listeners>
                    <add name="xyzListener"  type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Traces\xyz.txt" traceOutputOptions="DateTime" />
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="AllSwitch" value="All"/>
            <add name="OnlyErrors" value="Error"/>
        </switches>
        <trace autoflush="true" indentsize="3"/>
    </system.diagnostics>
    

    Finally, in order to use it:

    TraceUtilitiy trcSrc = new TraceUtility("xyz");
    
    trcSrc.TraceError("Error: {0}", "Your error description");
    

    Note that you can add trace methods for information and warnings to the trace utility class just as I did with the trace error methods.

    Hope this is what you are seeking for.

    EDIT:
    Your other code did not run because you did not specify a custom listener to the System.Diagnostics.Trace class. To do so add the following section into your config:

      <system.diagnostics>
        <trace autoflush="true" indentsize="4">
          <listeners>
            <remove name="DefaultTraceListener" />
    
            <add name="LogFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Traces\xyz1.txt" />
          </listeners>
        </trace>
      </system.diagnostics>
    

    and try using your old code. Hope this will de-baffle you 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
i want to parse a xhtml file and display in UITableView. what is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.