简体   繁体   English

如何在不显式调用被引用程序集的方法的情况下向其提供值?

[英]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. 但是,在创建日志记录类后,您只需要查找一次日志文件路径。

  1. 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. 这是相当可扩展的。

  2. 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.

相关问题 如何安全地验证WCF服务方法的调用程序集? - How do I securely authenticate the calling assembly of a WCF service method? 如何在没有项目或任何其他补充文件的情况下为引用的 .NET 程序集获取 IntelliSense? - How do I get IntelliSense for referenced .NET assembly without a project or any other supplemental file? 如何强制加载项目未明确使用的引用程序集 - How to force loading of an referenced assembly that is not explicitly used by the project 如何在没有明确指定或使用重载的情况下 Moq 在其签名中具有可选参数的方法? - How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload? 如何在Visual Studio中单步执行引用的程序集? - How do I step through a referenced assembly in Visual Studio? Roslyn:如何判断编译中是否引用了特定的程序集? - Roslyn: How do I tell if a particular assembly is referenced in a Compilation? 记录 dotnet core webapi 的扩展方法如何获取调用项目/程序集名称? - Extension method for logging dotnet core webapi how do I get the calling projects/assembly name? 如何手动编辑Visual Studio项目的清单以显式指定引用的程序集? - How do I manually edit the manifest of my Visual Studio project to explicitly specify referenced assemblies? 如何在不使用.NET显式调用Close Method的情况下关闭Sqlite Connection - How to Close Sqlite Connection without explicitly calling Close Method using .NET 未使用(和引用)的程序集上的程序集引用错误,为什么会出现此错误? - Assembly reference error on not used (and referenced) assembly, why do I this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM