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

  • Home
  • SEARCH
  • 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 6749317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:40:34+00:00 2026-05-26T12:40:34+00:00

We have a C# logging class that uses System.Diagnostics.StackTrace and StackFrame to obtain information

  • 0

We have a C# logging class that uses System.Diagnostics.StackTrace and StackFrame to obtain information about the filename, linenumber, class, and method on which logger.writeLine() was called, which are then inserted into the log message.

We’ve used this in console apps, in windows services, in .aspx web applications, and in .asmx web services, without problems.

I’m now working on an IIS-hosted WCF service, and I’m seeing something odd.

If I put a logger.writeLine() in the constructor of a ServiceBehavior class, and it gets to this code:

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
System.Diagnostics.StackFrame sf = st.GetFrame(2);

The first line seems to return null. Or, at least, the second throws a NullReferenceException.

Now here’s the weirdness. This only happens when I’m hosting the webservice in IIS. When I’m running in Visual Studio’s dev server, I have no problems.

And even in IIS, I have problems only in the constructor. Calls to logger.writeLine() work just fine, in the OperatioonContract methods and in methods called from them. It’s only in the constructor, and only when hosted by IIS, that there’s a problem.

If I wrap a try/catch around this, in logger.writeLine(), so that if the exception happens, we simply don’t include the line information in the log file, everything works fine. Or I could just not use logging in the constructor – the only thing I was doing in the constructor was logging that the constructor had been called. So I don’t actually have a problem.

But I’m wondering what is going on, and what odd corner of .NET I’ve stumbled into. When I run across oddities like this, and find work-arounds that seem to handle them, half the time there’s some other problem I just haven’t noticed yet.

So, does anyone have any explanations on why this might be happening?

  • 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-26T12:40:35+00:00Added an answer on May 26, 2026 at 12:40 pm

    When running under IIS you don’t have the privileges to examine code outside (above in the stack) your own assemblies.

    Anyway, you can easily make your code conditional by looking at st.FrameCount.

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

Sidebar

Related Questions

I have written a class that will handle internal logging in my application. Now
I have a framework that uses log4net for logging, I know that we can
I have a project that uses Apache Commons Logging & log4j with a large
I have a Java web application that uses the SLF4J logging facade. To date,
I have a logging class that, well, logs things. I would like to add
I have a Java web application that currently uses Log4J for logging. I'd like
I have created a GUI program that uses a class to perform some tasks.
I currently have a project that uses Entity Framework 4.1 for logging to a
I have a static object defined in my logging class, along the lines of:
At the company I work for, I have created a Error Logging class to

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.