在属性中设置记录器 [英] Set a logger in property

查看:62
本文介绍了在属性中设置记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下代码:

Say I have the following code:

protected ILogger Logger { get; private set; }
    private void CreateLogger(string clientConfigurationFilePath)
    {
		var composableLoggerFactory = new ComposableLoggerFactory();
		Logger = composableLoggerFactory
			.CreateComposableLogger(clientConfigurationFilePath);
    }





我不知道我们是否可以将该方法放入set assessor?



I don't know if we can can put the method into set assessor?

推荐答案

严格来说,这个问题毫无意义,因为我在评论中解释了这个问题。



问题是你必须传递字符串clientConfigurationFilePath ,这与常规属性的读取/赋值语义相矛盾。你应该明白,你所要求的只是使用一些语法糖,所以你想要实现的语法不能成为目标,只有保持语法甜蜜才能保存目的 - 对于您的类型的用户。 :-)



正式地,因为您需要传递文件路径参数,如果您创建索引属性this并将字符串设为索引:

Strictly speaking, the question makes no sense, by the reason I explained in my comment to the question.

The problem is that you have to pass string clientConfigurationFilePath, which contradicts the reading/assignment semantic of the "regular" property. You should understand that all you are asking is about the use of some syntactic sugar, so the syntax you want to implement cannot be a goal and would only save the purpose if you keep the syntax sweet — for the user of your type. :-)

Formally, as you need to pass the file path parameters, you can have something similar to property if you create an indexed property "this" and make the string an "index":
interface ILogger { /* ... */ }
class LoggerWrapper {
    protected internal virtual ILogger this[string fileName] {
        get { return logger; }
        set { logger = this.CreateLogger(fileName); }
    }
    ILogger logger;
    ILogger CreateLogger(string fileName) { /* ... */ }
}

// the usage will be ugly:
wrapper["someFileName"] = null; // ugly: in fact a logger is created
                                // and stored in the instance
                                // null (or anything else) does not matter
ILogger logger = wrapper["anything"]; // ugly: "anything" is the string
                                      // which does not matter

正式达成目标:您拥有属性语法和功能之前已经过了,但是它失败了具有良好语法糖的目的:用法是违反直觉的。请注意,他的代码不是我的幻想:这是你的想法的逻辑结果。整个想法并不富有成效,所以,让我们说,这只是其荒谬的例证。



(我添加内部使得最后的使用行成为可能,而 virtual 只是为了尝试至少建议它的远程目的,能够覆盖this。)



该怎么办?你应该知道的更好;这取决于你想要达到的目标。您的问题是:属性 Logger 完全符合方法 CreateLogger 的工厂使用。从类中删除此方法,并让用户从外部调用工厂方法。或保留代码,但是使 CreateLogger a protected public 方法,内部受保护。这取决于你想要从创建 Logger 实例的代码中得到什么。



主要的是:don尝试使用一些.NET / C#功能只是为了使用它。类型的API应该直接表示缩进。



-SA

Formally, the goal is reached: you have the property syntax and the functionality you already had before, but it defeats the purpose of having nice syntactic sugar: the usage is counter-intuitive. Note that his code is not my fantasy: this is the logical consequence of your idea. The whole idea is not productive, so, let's say, it was just the illustration of its absurdity.

(I added internal to make the usage lines at the end possible, and virtual just to try to suggest at least remote purpose of it, the ability to override "this".)

What to do instead? You should know better; it depends on what you wanted to achieve. Your problem is: the property Logger purely fits the factory use by the method CreateLogger. Remove this method from the class, and let the user to call the factory method externally. Or leave the code as is, but make CreateLogger a protected or public method, internal protected. It depends on what you want from the code which creates the Logger instance.

Main thing here is: don't try to use some .NET/C# feature just for the sake of using it. The type's API should directly express the indent.

—SA


这篇关于在属性中设置记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆