C# 动态设置appender文件路径的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/571876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to dynamically set an appender file path
提问by Dscoduc
I am trying to find somebody smarter than me to validate some syntax I wrote up. The idea is to configure the filename of my RollingFileAppender to the name of the assembly in order to make it more re-usable for my projects.
我试图找到比我更聪明的人来验证我写的一些语法。这个想法是将我的 RollingFileAppender 的文件名配置为程序集的名称,以使其更可重用于我的项目。
I've seen this previous SO articlebut it wasn't exactly able to answer my question...
我看过以前的 SO 文章,但它并不能完全回答我的问题......
I've had a dickens of a time trying to understand the inner components of Log4net and this is what I came up with (residing in the Global.asax file - Application_Start method):
我有一段时间试图理解 Log4net 的内部组件,这就是我想出的(驻留在 Global.asax 文件 - Application_Start 方法中):
// Bind to the root hierarchy of log4net
log4net.Repository.Hierarchy.Hierarchy root =
log4net.LogManager.GetRepository()
as log4net.Repository.Hierarchy.Hierarchy;
if (root != null)
{
// Bind to the RollingFileAppender
log4net.Appender.RollingFileAppender rfa =
(log4net.Appender.RollingFileAppender)root.Root.GetAppender("RollingLogFileAppender");
if (rfa != null)
{
// Set the file name based on the assembly name
string filePath =
string.Format("~/App_Data/{0}.log", GetType().Assembly.GetName().Name);
// Assign the value to the appender
rfa.File = Server.MapPath(filePath);
// Apply changes to the appender
rfa.ActivateOptions();
}
}
Can anyone tell me, 'this is hideous', or 'this should work fine'? Also, if I set the file dynamically can I still expect the log4net behavior to rotate the files based on the log4net.config file settings?
谁能告诉我,“这太可怕了”或“这应该可以正常工作”吗?另外,如果我动态设置文件,我是否仍然可以期望 log4net 行为根据 log4net.config 文件设置轮换文件?
Much appreciated!
非常感激!
采纳答案by Eddie
You are doing this the hard way! Define your log4net config as XML in your application's configuration file and use %property{}
to advantage:
你这样做很困难!在应用程序的配置文件中将 log4net 配置定义为 XML 并使用%property{}
:
<appender name="YourAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="~/App_Data/%property{LogName}" />
....
</appender>
This is dynamic -- you just have to set the log4net property "LogName
" beforeyou initialize log4net. Thus, in your code any time before you configure log4net, set the desired value of this property:
这是动态的——您只需要在初始化 log4net之前设置 log4net 属性“ LogName
” 。因此,在您配置 log4net 之前的任何时间,在您的代码中,设置此属性的所需值:
string LogName = GetType().Assembly.GetName().Name + ".log";
log4net.GlobalContext.Properties["LogName"] = LogName;
Of course, you may use any property name. I've chosen "LogName" for a simple example, but you can have one per application if you want, as long as your code knows what the correct property name is and what the correct value should be.
当然,您可以使用任何属性名称。我选择了“LogName”作为一个简单的示例,但如果您愿意,您可以为每个应用程序设置一个,只要您的代码知道正确的属性名称是什么以及正确的值应该是什么。
回答by Sebastian H?ni
In 2015, we do it like this:
2015年,我们是这样做的:
<file type="log4net.Util.PatternString">
<conversionPattern value="%appdomain.log" />
</file>
No other code required.
不需要其他代码。
App domain is the executing assembly's file name.
应用程序域是执行程序集的文件名。
回答by Peter J. de Bruin
Here is way to set or change the logfile of the first appender at runtime:
以下是在运行时设置或更改第一个 appender 的日志文件的方法:
var appender = (log4net.Appender.FileAppender)LogManager.GetRepository().GetAppenders()[0];
appender.File = "C:\whatever.log";
appender.ActivateOptions();
回答by jhovanny gonzalez
it worked for me with the date
<file type="log4net.Util.PatternString" value="./Log/logQueueService%date{yyyy_MM_dd}.log" />
它对我有用
<file type="log4net.Util.PatternString" value="./Log/logQueueService%date{yyyy_MM_dd}.log" />