我正在尝试使用我的记录器类库从实际应用程序的App.Config中获取一些配置设置。
以下是记录器库中的助手类:
class Main{
func main(){
print(Value.a)
Value.a++
}
}
class OtherClass{
func otherMain(){
print(Value.a)
Value.a++
}
}
let main = Main()
let other = OtherClass()
//I want this call to print 0
main.main()
//I want this call to print 1
other.otherMain()
更新
从我的App.Config剪辑:
public class SettingsHelper
{
private static readonly Configuration Config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
public static void CheckLogSettings()
{
string key;
key = "LoggingEnabled";
if (Config?.AppSettings?.Settings?[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, true.ToString());
Config.Save(ConfigurationSaveMode.Modified, false);
}
key = "MaxLogFileSize";
if (Config.AppSettings.Settings[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, (2*1024*1024).ToString());
Config.Save(ConfigurationSaveMode.Modified, false);
}
key = "LogFileName";
if (Config.AppSettings.Settings[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, "AppLog.txt");
Config.Save(ConfigurationSaveMode.Modified, false);
}
}
public static bool GetLoggingEnabled()
{
return bool.Parse(Config.AppSettings.Settings["LoggingEnabled"].Value);
}
public static long GetMaxLogFileSize()
{
return long.Parse(Config.AppSettings.Settings["MaxLogFileSize"].Value);
}
public static string GetLogFileName()
{
return Config.AppSettings.Settings["LogFileName"].Value;
}
答案 0 :(得分:0)
解决方案是手动添加到App.Config:
<appSettings>
<add key="LoggingEnabled" value="True"/>
<add key="MaxLogFileSize" value="2097152"/>
<add key="LogFileName" value="AppLog.txt"/>
</appSettings>
然后Tim指出:ConfigurationManager.AppSettings["SomeKey"]
将在您的类库中运行。