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 8083763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:34:44+00:00 2026-06-05T17:34:44+00:00

When writing code, I often place debug messages in the code. The debug messages

  • 0

When writing code, I often place debug messages in the code. The debug messages are handled by a logger class, which outputs the messages to file in debug mode and discards them in release modes.

It looks a little something like this:

class Logger : IDisposable
{
    private StreamWriter m_Logger = null;

    public void Start(string logFile)
    {
        m_Logger = new StreamWriter(logFile);
        m_Logger.AutoFlush = true;
    }

    public void Dispose()
    {
        if (m_Logger != null) m_Logger.Dispose();
    }

    public void WriteLine(string message)
    {
        if (m_Logger != null) m_Logger.WriteLine(message);
    }
}

An instance is created at startup, and is accessible from the Program class. I then check for debug like this:

#if DEBUG
Program.Log.Start("app.log");
#endif

This works great, in that it dumps debug info when in debug mode, and doesn’t in release mode. However, if I run the release executable through a utility such as strings, I can still see the debug strings. I’d prefer to keep them out of the release build entirely, to help prevent reverse engineering.

The only solution I’ve found so far is to wrap all debug messages in preprocessor conditionals:

// < some code here >
#if DEBUG
Program.Log.WriteLine("Some debug message.");
#endif
// < more code here >

This is pretty tedious and ugly. My first thought was to use some sort of preprocessor macro, but C# doesn’t support them. Is there a more elegant solution than the one I’m using right now?

  • 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-06-05T17:34:46+00:00Added an answer on June 5, 2026 at 5:34 pm

    To avoid using #if-#endif on each WriteLine call, try using ConditionalAttribute on the logger method itself:

    [Conditional("DEBUG")]
    public void WriteLine(string message)
    {
        if (m_Logger != null) m_Logger.WriteLine(message);
    }
    

    This will be excluded from MSIL in case of a Release build.

    Applying ConditionalAttribute to a method indicates to compilers that
    a call to the method should not be compiled into Microsoft
    intermediate language (MSIL) unless the conditional compilation symbol
    that is associated with ConditionalAttribute is defined. Applying
    ConditionalAttribute to an attribute indicates that the attribute
    should not be emitted to metadata unless the conditional compilation
    symbol is defined

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

Sidebar

Related Questions

While striving for const-correctness, I often find myself writing code such as this class
When we write code which helps the business, we create value for them, often
I find myself writing this class often in my python code when I need
I often end up writing code like if x == 1 or x ==
When I'm writing code with QtCreator (2.4.1), I'm very often in this situation: something
When writing C++ code, I often start by writing full 'implementation' code in my
Code is read more often then updated. Writing more readable code is better than
I often end up writing a bit of code twice when using a loops.
While writing code in a file that would comprise of PHP, HTML, CSS &
Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses();

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.