c# - Pass a logger to a static class -


i have static class in production. reason, need replace logging part methods in class.(still static class unchanged)

now want pass logger it. logger class.

public static class outpututils        public static void loginfo(string logmessage)     {         logger.loginfo(logmessage);     } 

in class, have

private readonly ilogger _logger;     public anotherclass(ilogger logger)     {         _logger = logger;     }     internal static void logdebug(string logmessage)     {         outpututils.loginfo(logmessage);     } 

yes, want pass _logger static class outpututils. how?

the simplest change add logger parameter:

public static void loginfo(ilogger logger, string logmessage) {     logger.loginfo(logmessage); } 

i not add static property class, because have consider thread-safety if multiple clients try set property. have race conditions if 1 thread sets logger, thread sets property other logger, , first thread calls loginfo, output other thread's logger.

but what's purpose of method @ all? why doesn't client call logger.loginfo(logmessage)?


Comments