如何处理静态类中错误的 appSettings?

huangapple go评论67阅读模式
英文:

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 or false if application.config's entry is TRUE or FALSE (case insensitive is preferred).
  • false if application.config's entry is anything else.
  • false if application.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.

huangapple
  • 本文由 发表于 2023年4月4日 15:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定