[英]How do I supply values to an referenced assembly without calling a method it explicitly?
Currently I have a static class that I use as my logging module. 当前,我有一个静态类用作日志记录模块。 I've added the class to my visual studio solution.
我已将该类添加到我的Visual Studio解决方案中。 Within the class I've specified the name and location of the log file to use.
在该类中,我指定了要使用的日志文件的名称和位置。 Which lets me do stuff like this – which I like and want.
这样我就可以做我喜欢和想要的事情。
Logger.Information(“Page_Load”,”controls loaded correctly”);
I'd like to refactor the code and move the logging functionality into a separately compiled assembly, if I did this I would then need to pass in the log file name and location to save the files too. 我想重构代码并将日志记录功能移至单独编译的程序集中,如果这样做,则还需要传递日志文件名和位置以保存文件。
However I don't want to have to supply this information every time I call the 'Logging' method, this would be bad... 但是,我不想每次调用“ Logging”方法时都必须提供此信息,这很糟糕……
Logger.Informtaion(“Page_Load”,”controls loaded correctly”,”logfile.txt”,”c:\temp”);
Is there any way I can supply this information without having to specify it within each page or via the method call. 有什么方法可以提供此信息而不必在每个页面中或通过方法调用进行指定。
For sure. 当然。 The simplest thing would be to add a single key to the web.config file, which your class looks at by using the ConfigurationManager.
最简单的事情是将单个密钥添加到web.config文件中,您的班级将使用ConfigurationManager查看该密钥。
<configuration>
<appSettings>
<add key="logfile" value="c:\log.txt" />
</appSettings>
<system.web>
...
</system.web>
</configuration>
string logfile = ConfigurationManager.AppSettings["logfile"]
If your logging class is more complex than just one or two configuration options, consider building a ConfigurationSection class to go along with it, which would allow you to create your own section in the web.config. 如果您的日志记录类比一个或两个配置选项更复杂,请考虑构建一个ConfigurationSection类以与其一起使用,这将使您可以在web.config中创建自己的部分。
The configuration approach is good for this type of thing, because then you avoid hard-coding the logfile path into your application code (such as passing it into a static initialization method), which would require a recompile if you needed to change the logging path. 配置方法对这类事情很有用,因为这样可以避免将日志文件路径硬编码到应用程序代码中(例如,将其传递到静态初始化方法中),如果需要更改日志记录路径,则需要重新编译。 However, you should only need to look up the logfile path once, upon creation of your logging class.
但是,在创建日志记录类后,您只需要查找一次日志文件路径。
I feel obliged to ask if you've investigated using TraceListener and the System.Diagnostics namespace with its built-in logging (as opposed to rolling your own). 我觉得有义务问您是否使用TraceListener和System.Diagnostics命名空间及其内置的日志记录进行了调查(而不是自己滚动)。 It's quite extensible.
这是相当可扩展的。
Have you considered making your logger non-static? 您是否考虑过使记录器为非静态? Perhaps a singleton?
也许单身? This is the classic example for an appropriate use of the Singleton pattern.
这是适当使用Singleton模式的经典示例。
Sure, 当然,
Create an initialize
method on your static class and pass the file into the initialize mentod. 在您的静态类上创建一个
initialize
方法,然后将文件传递到initialize mentod中。 Then call the method at application start. 然后在应用程序启动时调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.