Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. global debug variable?

global debug variable?

Scheduled Pinned Locked Moved C#
questiondebugging
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkfish
    wrote on last edited by
    #1

    I have logging code that I want to set a global variable for the whole application, something like bool logging = true; I don't want to have to go to all my classes and set this variable. What is the best way to handle this? Can I set it in the web.config file? If so, how? Thanks!

    J S A 3 Replies Last reply
    0
    • S sharkfish

      I have logging code that I want to set a global variable for the whole application, something like bool logging = true; I don't want to have to go to all my classes and set this variable. What is the best way to handle this? Can I set it in the web.config file? If so, how? Thanks!

      J Offline
      J Offline
      Jim Stewart
      wrote on last edited by
      #2

      You could use compiler directives:

      #if DEBUG
      // logging code here
      #endif

      This has the benefit of being compiled out when you go to release code.

      α.γεεκ

      Fortune passes everywhere.
      Duke Leto Atreides

      1 Reply Last reply
      0
      • S sharkfish

        I have logging code that I want to set a global variable for the whole application, something like bool logging = true; I don't want to have to go to all my classes and set this variable. What is the best way to handle this? Can I set it in the web.config file? If so, how? Thanks!

        S Offline
        S Offline
        scadaguy
        wrote on last edited by
        #3

        Take a look at the Trace and TraceSwitch classes. Brian

        1 Reply Last reply
        0
        • S sharkfish

          I have logging code that I want to set a global variable for the whole application, something like bool logging = true; I don't want to have to go to all my classes and set this variable. What is the best way to handle this? Can I set it in the web.config file? If so, how? Thanks!

          A Offline
          A Offline
          Alvaro Mendez
          wrote on last edited by
          #4

          One approach is to create a class that handles the logging and keeps track of whether it's enabled or not, like this:

          class Log
          {
          private static bool m_enabled = System.Configuration.ConfigurationSettings.AppSettings["Logging"] == "true";

          public static bool Enabled
          {
          get { return m_enabled; }
          }

          public static void Error(string message)
          {
          if (Enabled)
          // do the logging here
          }

          public static void Info(string message)
          ....
          }

          Then you can call Log.Error, Log.Info, etc. in your code. For more details on how to use web.config, check out MSDN[^]. Regards, Alvaro


          Hey! It compiles! Ship it.

          S 1 Reply Last reply
          0
          • A Alvaro Mendez

            One approach is to create a class that handles the logging and keeps track of whether it's enabled or not, like this:

            class Log
            {
            private static bool m_enabled = System.Configuration.ConfigurationSettings.AppSettings["Logging"] == "true";

            public static bool Enabled
            {
            get { return m_enabled; }
            }

            public static void Error(string message)
            {
            if (Enabled)
            // do the logging here
            }

            public static void Info(string message)
            ....
            }

            Then you can call Log.Error, Log.Info, etc. in your code. For more details on how to use web.config, check out MSDN[^]. Regards, Alvaro


            Hey! It compiles! Ship it.

            S Offline
            S Offline
            sharkfish
            wrote on last edited by
            #5

            Thanks Alvaro. That is exactly what I was looking for. I will keep the other suggestions in mind for other things, however. Cheers.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups