Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, July 31, 2015 3:26 PM
I have a logger.
private static ILogger _logger;
To create it, I have the method:
private static void CreatePayTelLogger(string clientConfigurationFilePath)
{
var composableLoggerFactory = new ComposableLoggerFactory();
_logger = composableLoggerFactory.CreateComposableLogger(clientConfigurationFilePath);
}
Now I have a static class in the production code, there is no constructor in this class. All the methods are directly called. How to create the logger in this class?
public static class ConfigHelpers
{
private static ILogger _logger;// I want to create and use it in this static class.
public static int loadIntegerSetting(string settingNameInt, int defaultValueInt)
{
int outputVar;
if (!int.TryParse(ConfigurationManager.AppSettings[settingNameInt] ?? defaultValueInt.ToString(), out outputVar))
{
if (outputVar == 0) outputVar = defaultValueInt;
_logger.LogInfo("Defaulting " + settingNameInt + " App.config setting to '" +
defaultValueInt + "' due to config read error.");
}
else
_logger.LogInfo(settingNameInt + " set to " + outputVar + ".");
return outputVar;
}
public static bool loadBoolSetting(string settingNameBool, bool defaultValueBool)
{
bool outputVar;
string parseText = ConfigurationManager.AppSettings[settingNameBool] ?? defaultValueBool.ToString(); // "false";
if (parseText.Substring(0, 1).ToUpper() == "Y") parseText = "true";
else if (parseText.Substring(0, 1).ToUpper() == "N") parseText = "false";
if (!bool.TryParse(parseText, out outputVar))
{
if (!outputVar) outputVar = defaultValueBool;
_logger.LogInfo("Defaulting " + settingNameBool + " App.config setting to '" +
defaultValueBool + "' due to config read error.");
}
else
_logger.LogInfo(settingNameBool + " set to " + outputVar + ".");
return outputVar;
}
Thanks,
All replies (1)
Friday, July 31, 2015 3:35 PM âś…Answered
Static classes can have static constructors, if that helps. A static constructor would be automatically called before any static members are referenced, although you have no control as to exactly when it would get called.