¿Hay alguna forma de agregar %APPDATA% a la ruta de configuración de Log4Net en AssemblyInfo.cs?
Frecuentes
Visto 1,101 veces
1
I am configuring log4net to use a separate configuration file. This can be done by adding the following line in your AssemblyInfo.cs file.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
This location is relative to the program directory, yet I want it in a user's APPDATA folder. Something like:
ConfigFile = @"C:\Users\UserName\AppData\Roaming\MyApplication\Logging\Log4Net.config"
The problem is that this is not a relative path, i.e., a different usernames are not supported etc. What I really want is the following:
ConfigFile = @"%APPDATA%\MyApplication\Logging\Log4Net.config"
This of course does not work. I was hoping that this might fix it:
ConfigFile = @"${APPDATA}\MyApplication\Logging\Log4Net.config"
o esto:
ConfigFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
@"MyApplication\Logging\Log4Net.config")
No luck. The two first just thinks it is part of a relative path, the latter is refused since it is in AssemblyInfo:
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type C:\Source\ ... \Tools\Logging\Logging\Properties\AssemblyInfo.cs 39 56 Logging
Do I have to configure the application to use the custom path at startup? I really don't want to, because I want the code to be as agnostic as possible.
1 Respuestas
0
I ended up performing the configuration at startup. In the implementation of my logging facade I just said:
class Log4NetLogger : ILogger
{
public Log4NetLogger(...)
{
if (!m_Configured){
string configFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MyApplication\Logging\Log4Net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
m_Configured = true;
}
...
}
...
}
Respondido 28 ago 12, 10:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# .net logging log4net relative-path or haz tu propia pregunta.
I guess I shouldn't hard code the "MyApplication\Logging\Log4Net.config" path either. - André C. Andersen