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 Logger instance

Global Logger instance

Scheduled Pinned Locked Moved C#
designregexarchitecturequestion
4 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.
  • A Offline
    A Offline
    albean
    wrote on last edited by
    #1

    I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?

    L J S 3 Replies Last reply
    0
    • A albean

      I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      static methods :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      1 Reply Last reply
      0
      • A albean

        I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?

        J Offline
        J Offline
        Julian Bucknall MSFT
        wrote on last edited by
        #3

        Write a logger class that has a single static method: public class Logger { static public Log(string msg); } Then you would call the Log method like this: Logger.Log("Sent a message"); Of course the logger class would have a bunch of private methods and classes underneath to use a logging instance of some type that does the work. An alternative is to write a classic Singleton. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

        1 Reply Last reply
        0
        • A albean

          I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?

          S Offline
          S Offline
          StealthyMark
          wrote on last edited by
          #4

          Inherit a class from System.Diagnostics.TraceListener. On application start, add an instance of this class to the System.Diagnostics.Trace.Listeners and/or System.Diagnostics.Debug.Listeners collections. Every time you use Trace.Writexxx or Debug.Writexxx, it will be logged through your class. If you change/expand your logging facilities at some time, you don't have to change any client code, just your TraceListener.

          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