Correction:
nav.SetValue("test.eng.fsu.edu");
should be:
nav.SetValue(smtpHost);
----- Original Message ----
From: Duder Himer <[EMAIL PROTECTED]>
To: Log4NET User <[email protected]>
Sent: Wednesday, November 28, 2007 11:30:15 AM
Subject: Re: Programmatically Change Configuration?
Thought I'd share this for anyone else who has a similar problem, since it took
quite a bit of trial and error to get it to work right. This can probably be
used for just about any log4net setting in a App.config file. The main
difference would be the xpath query string. The below will take any appender's
of type "log4net.Appender.SmtpAppender,log4net" and change their smtpHost
value. It could probably be modified to Create the smtpHost element if not
found as well, and be made more generic of course.
private void changeConfigTest()
{
System.Configuration.Configuration config =
System.Configuration.ConfigurationManager.OpenExeConfiguration(
System.Configuration.ConfigurationUserLevel.None);
System.Configuration.ConfigurationSection smtpAppenderSection =
config.Sections["log4net"];
string xml = smtpAppenderSection.SectionInformation.GetRawXml();
string newXml = setSmtpHost(xml,"NEW.smtp.com");
smtpAppenderSection.SectionInformation.SetRawXml(newXml);
smtpAppenderSection.SectionInformation.ForceSave = true;
config.Save();
}
private string setSmtpHost(string log4netXml, string smtpHost)
{
XmlDocument document = new XmlDocument();
document.LoadXml(log4netXml);
System.Xml.XPath.XPathNavigator navigator = document.CreateNavigator();
foreach (System.Xml.XPath.XPathNavigator nav in navigator.Select(
"/log4net/[EMAIL
PROTECTED]'log4net.Appender.SmtpAppender,log4net']/smtpHost/@value"
) )
{
nav.SetValue("test.eng.fsu.edu");
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
System.IO.StringWriter stringStream =
new System.IO.StringWriter();
XmlWriter writer = XmlTextWriter.Create(stringStream, settings);
document.Save(writer);
return stringStream.ToString();
}
----- Original Message ----
From: Duder Himer <[EMAIL PROTECTED]>
To: Log4NET User <[email protected]>
Sent: Tuesday, November 27, 2007 1:59:05 PM
Subject: Re: Programmatically Change Configuration?
Well, I think I got half of it figured out. It seems the
XmlHierarchyConfigurator class does most of the grunt work of reading
the xml configuration. I was hoping it was using some sort of
deserialization, which would allow me to probably find a way to use
serialization to save settings, but this is not the case. It seems to use the
xml element names and find writable properties in the corresponding objects to
write the values to. I could do the opposite, where I read through appenders,
and where I find writable properties, write them to the xml, but I wouldn't
want to write them all out. I should only write those out which are different
from the default, so I'd have to compare the default to the actual and only
write when they are different.
I think
writing directly to the Xml file to make changes is going to be my best
bet considering the small amount of changes I want to make. It just always
seems dirty and risky to parse through xml manually.
----- Original Message ----
From: Duder Himer <[EMAIL PROTECTED]>
To: Log4NET User <[email protected]>
Sent: Tuesday, November 27, 2007 1:18:19 PM
Subject: Re: Programmatically Change Configuration?
Thanks Napo,
That code would reload the configuration file, correct?
I am wanting to change a specific setting, such as the Smtp host, and have that
save to the configuration. Having a seperate configuration file is fine, and
is probably a good idea. I was hoping to figure out how log4net manages to map
xml elements to properties, and then I would attempt to reverse the process for
saving the data.
----- Original Message ----
From: "Chen, Xuguang" <[EMAIL PROTECTED]>
To: Log4NET User <[email protected]>
Sent: Wednesday, November 21, 2007 8:24:30
PM
Subject: RE: Programmatically Change Configuration?
Message
DIV {
MARGIN:0px;}
Hi,Snozz:
I reset my log4net configuration in
global.asax for asp.net project.
string serverMapPath =
Server.MapPath("~");
string configFile = serverMapPath +
ConfigurationManager.AppSettings["Log4NetPath"];
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo(configFile));
Hope can help you.
Napo.chen
-----Original Message-----
From: Duder Himer
[mailto:[EMAIL PROTECTED]
Sent: 2007年11月21日 4:36
To:
[email protected]
Subject: Programmatically Change
Configuration?
Is there a way to programmatically change the configuration through the
log4net API? For example, when my application is run for the first time,
I want to allow the user to provide their smtp host for the SmtpAppender, and
have that information saved to the configuration.
I'm trying to read
through the log4net code the loads the configuration to see if there is
something there I can make use of, but I'm getting a little lost. I am
not familier with NUnit, thus I don't have the source setup to compile such
that I can step though the code. So I am manually looking through the
code, starting at the default XmlConfigurator.Configure() method, and jumping
through the defines of the methods it calls. It gets really confusing
when the GetRepository functions are using interfaces, and I have to figure
out which implementation of that interface would actually be used so that I
can look through the functions being called.
Thanks in
advance.
-Snozz
Be a better pen pal.
Text or chat with friends inside Yahoo! Mail. See how.
Be a better sports nut! Let your teams follow you
with Yahoo Mobile. Try it now.
____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs