1
我想在global.asax中以編程方式設置log4net securitycontext,但它不起作用。如果我把這個關鍵字放在web.config中,效果很好,但是我想從數據庫中獲取這些值。如何以編程方式爲log4net設置securityContext?
我的web.config的作品:
<log4net>
<appender name="FileAppenderGeneral" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\app.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="'.'dd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p - %m%n" />
</layout>
<<securityContext type="log4net.Util.WindowsSecurityContext">
<domainName value="hostname"/>
<userName value="username" />
<password value="password" />
</securityContext>
<encoding value="utf-8" />
</appender>
我的Global.asax不是工作:
log4net.Config.XmlConfigurator.Configure();
ILog[] loggers = LogManager.GetCurrentLoggers();
foreach (ILog logger in loggers)
{
foreach (IAppender appender in logger.Logger.Repository.GetAppenders())
{
if (appender.GetType() == typeof(RollingFileAppender))
{
log4net.Appender.RollingFileAppender fileAppender = (RollingFileAppender)appender;
WindowsSecurityContext securityContext = new log4net.Util.WindowsSecurityContext();
securityContext.DomainName = "hostname";
securityContext.UserName = "username";
securityContext.Password = "password";
securityContext.ActivateOptions();
fileAppender.SecurityContext = securityContext;
}
}
}
任何想法,那我在Global.asax做壞事?
Finnally我這樣做你說,創建我的appender在C#中,而不是在web.config。 Thnak你! – Mikelon85
很高興幫助! – MaKCbIMKo