英文:
How to deal with wrong appSettings in a static class?
问题
以下是翻译的部分:
我正在使用一个静态类,该类应决定是否记录信息。
原始代码如下:
public static bool ShouldLog { get; set; } = true;
我决定使用应用程序设置来实现这一点:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"];
这不起作用,因为`ConfigurationManager.AppSettings[]`返回一个字符串,所以需要进行解析:
public static bool ShouldLog { get; set; } =
Boolean.Parse(ConfigurationManager.AppSettings["ShouldLog"]);
这也不好,因为当应用程序配置文件中未填写任何内容时,会生成`NullReferenceException`异常,因此尝试如下:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"] == null ? false :
Boolean.Parse(ConfigurationManager.AppSettings["ShouldLog"]);
这也不好,因为当有人在应用程序配置文件中输入了无效数据时,会生成`TypeInitializationException`异常,因此尝试如下:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"] == null ? false :
Boolean.TryParse(ConfigurationManager.AppSettings["ShouldLog"]);
这也不起作用,因为签名不是`result = TryParse()`而是`TryParse(..., out result)`,所以现在我已经没有更多选项:
我希望有类似这样的解决方案:
bool ShouldLog = ...(ConfigurationManager.AppSettings["ShouldLog"]);
期望结果:
- 如果`application.config`中的条目为`TRUE`或`FALSE`(不区分大小写),则返回`true`或`false`。
- 如果`application.config`中的条目为其他任何内容,则返回`false`。
- 如果`application.config`不包含期望的条目,则返回`false`。
英文:
I'm working with a static class, which should decide whether or not to log information.
It started with:
public static bool ShouldLog { get; set; } = true;
I decided to use an application setting for this:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"];
This does not work, because ConfigurationManager.AppSettings[]
returns a string, so this needs to be parsed:
public static bool ShouldLog { get; set; } =
Boolean.Parse(ConfigurationManager.AppSettings["ShouldLog"]);
This is also not good, because, when nothing is filled in in the application.config file, this will generated a NullReferenceException
, hence the next attempt:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"] == null ? false :
Boolean.Parse(ConfigurationManager.AppSettings["ShouldLog"]);
This is also not good, because when somebody has entered nonsense into the application.config file, this will generate a TypeInitializationException
exception, so the next attempt:
public static bool ShouldLog { get; set; } =
ConfigurationManager.AppSettings["ShouldLog"] == null ? false :
Boolean.TryParse(ConfigurationManager.AppSettings["ShouldLog"]);
This does also not work, because the signature is not result = TryParse()
but TryParse(..., out result)
, so now I'm out of options:
I would like something like:
bool ShouldLog = ...(ConfigurationManager.AppSettings["ShouldLog"]);
Expected result :
true
orfalse
ifapplication.config
's entry isTRUE
orFALSE
(case insensitive is preferred).false
ifapplication.config
's entry is anything else.false
ifapplication.config
does not contain the expected entry.
答案1
得分: 2
要实现您所要求的精确目标,您可以使用以下代码:
public static bool ShouldLog => ConfigurationManager.AppSettings["ShouldLog"]
?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
但是,我个人建议您使用一个现有的常用日志框架,该框架提供配置支持,而不是自己编写日志功能。这很可能会提供更多的灵活性,避免重复造轮子。
英文:
Well, to achieve the precise goal of what you're asking, you could use:
public static bool ShouldLog => ConfigurationManager.AppSettings["ShouldLog"]
?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
However, I'd personally recommend that instead you use an existing, commonly-used logging framework that provided configuration support, instead of rolling your own. That's likely to provide much more flexibility, as well as avoiding reinventing the wheel.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论