Why is the XML node's parent node being changed? - c#

I have searched on here and Google for an answer to this but I cannot find a thing, apologies if this is something simple.
I have the below code:
private XmlNode loadXMLNode(string configPath, string loggingPath)
{
configFile.Load(configPath);
XmlNode logging = configFile.DocumentElement.SelectSingleNode(loggingPath);
return logging;
}
public string testXMLNodeValue(string configPath, string loggingPath)
{
XmlNode logging = loadXMLNode(configPath, loggingPath);
string value = logging.Attributes["value"].Value;
return value;
}
public void setXMLNodeValue(string configPath, string loggingPath, string newValue)
{
XmlNode logging = loadXMLNode(configPath, loggingPath);
logging.Attributes["value"].Value = newValue;
MessageBox.Show(logging.Attributes["value"].Value);
configFile.Save("C:\\inetpub\\CiresonPortal\\bin\\Cireson.CacheBuilder.Service.exe.config");
}
Which does correctly load the XML file and does modify the value in the targeted node. However it also changes the name of the parent node and I have no idea why.
(If you're wondering why the final path is hard coded it is because it was not saving the file at all when using the path from the configuration file, but that is a question for another thread.)
It should look like this:
<logger name="Logger">
<!-- LEVELS: ALL, DEBUG, INFO, WARN, ERROR, FATAL-->
<!-- Suggested for debugging: INFO -->
<!-- Suggested for production: WARN -->
<level value="ERROR" />
However once the above code runs it looks like this:
<root>
<level value="ERROR" />
Any help would be very much appreciated as I am still teaching myself c# (coming from a PowerShell background).
Thank you.

Related

How to read web.config section as XML in C#?

This is copied example from:
How to read custom config section in app.config in c#
I want to read following custom section from app.config:
<StartupFolders>
<Folders name="a">
<add folderType="Inst" path="c:\foo" />
<add folderType="Prof" path="C:\foo1" />
</Folders>
<Folders name="b">
<add folderType="Inst" path="c:\foo" />
<add folderType="Prof" path="C:\foo1" />
</Folders>
</StartupFolders>
And this is my case too. However, I don't want to create custom class for handling values, defining this class in web.config, and then finally using it. It is heavy-weight for my needs.
Instead I would like to do something very simple -- retrieve a section as XML. Then I could use regular Linq.Xml to parse it. This way, I don't need to create new classes per each section, I don't need to declare them. For my purpose it is sufficient on one hand, and minimal at the other (I do it once, key-value mapper for nested sections). I.e. perfect.
The only missing piece is (my question) -- how to get a web.config section as XML?
Note about the section:
it cannot be encoded, because it has to be edited by hand
it cannot be serialized for the same reason
So I am not looking for a workaround how to squeeze entire section as value in appSettings, but I am really looking for a method to get proper section as XML.
I would like to get it from ConfigManager (!), because this way I don't have to deal with resolving which web.config should I read, etc. I.e. less chance to make mistake than mimicing web.config precedence manually.
Forgive me for reminding this, but please avoid "answers", you shouldn't do this, use custom class per each section, etc. I already considered this, and opted against it.
I think you either have to do it manually and load the Web config into memory:
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
Or you will need to create the custom configuration sections you want to avoid.
You can define a re-usable custom config section that exposes the section XML as you desire. The key is, that you don't have to define a different class for each custom config section.
For clarity, my project namespace is "ConsoleApp1" as is the assembly name (this appears in type definitions).
First, create a custom config section that exposes the XML reader:
public class XmlConfigSection : ConfigurationSection
{
public XmlReader Xml { get; private set; }
override protected void DeserializeSection(XmlReader reader)
{
Xml = reader;
}
}
You can then define any of your custom sections to use this class in the app.config:
<configSections>
<section name="StartupFolders" type="ConsoleApp1.XmlConfigSection, ConsoleApp1" />
<section name="AnotherCustomSection" type="ConsoleApp1.XmlConfigSection, ConsoleApp1" />
</configSections>
Then in your code, you can access raw XmlReader of the config section like this:
var xmlReader = (ConfigurationManager.GetSection("StartupFolders") as XmlConfigSection).Xml;
If you then want an XML string instead of the reader you can do something like this (though I'd suggest sticking with the XmlReader):
StringBuilder sb = new StringBuilder();
while (xmlReader.Read())
sb.AppendLine(xmlReader.ReadOuterXml());
var xmlStr = sb.ToString();
Totally untested but could you use something like this? :
ConfigurationSection exampleSection =
(ConfigurationSection)ConfigurationManager
.GetSection("system.web/exampleSection");
Then possibly use exampleSection.ElementInformation to get more info?

How to access property summary in code?

I have a class, with few properties, where some of the have an XML comment (summary).
What I want to do is display the summary info for the user in the application.
So I need to access summary text in code, to be able to do : Label1.Text = .......
how do I do that ?
public class MyObject
{
public int ID { get; set; }
/// <summary>
/// very very very very extensive information about the city
/// </summary>
public string City { get; set; }
public DateTime Date { get; set; }
public int Value { get; set; }
public int DiffToPrev { get; set; }
}
class Program
{
static void Main()
{
var a = new MyObject();
var t = a.GetType().GetProperty("City");
Console.WriteLine(t....................
Note that XML comments are not included in resulting exe/dll file, so you'll need to enable xml file generation and distribute them too. Consder using attributes to provide run-time available information on your properties/methods/classes/etc, as XML comments probably were not designed for what you are trying to do.
Anyway, XML comments have following format:
<?xml version="1.0"?>
<doc>
<assembly>
<name><!-- assembly name here --></name>
</assembly>
<members>
<!-- ... -->
<member name="M:Full.Type.Name.PropertyName">
<summary>
<!-- property summary here -->
</summary>
</member>
<!-- ... -->
</memebers>
</doc>
So if you still want it, you need to load your XML comments file and find XML node, describing your property (untested code, just to show approach):
var a = new MyObject();
var t = a.GetType().GetProperty("City");
string xmlMemberName = "M:" + a.FullName + t.Name;
var xmlDoc = new XmlDocument();
xmlDoc.Load("you_xml_comments_file.xml");
var membersNode = xmlDoc.DocumentElement["members"];
string summary = "";
if(membersNode != null)
{
foreach(XmlNode memberNode in membersNode.ChildNodes)
{
if(memberNode.Attributes["name"].Value == xmlMemberName)
{
summary = memberNode["summary"].InnerText;
break;
}
}
}
Console.WriteLine(summary);
Update: You can also include your XML comments file as an embedded resource so you'll never forget to distribute it, or even write a small tool, which transforms XML comments file into .resx XML resources file.
Incuding XML comments file as an embedded resource:
enable XML file generation in project properties
set XML output file path to project directory (instead of bin/Release or bin/Debug)
compile project
in project explorer enable "show all files" and inlude generated xml file
open file properties and set build action to "Embedded Resource"
Now your xml comments are included in assembly file as resource an can be loaded this way:
XmlDocument doc;
using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"your_default_namespace" + "." + "your_xml_file_name.xml"))
{
doc.Load(stream);
}
To generate .resx from comments XML file:
.resx format:
<root>
<!-- some header stuff which can be copy-pasted from any other valid .resx -->
<!-- ... -->
<data name="Your_Object_Full_Name_PropertyName" xml:space="preserve">
<value><!-- summary here --></value>
</data>
<!-- ... -->
</root>
Strings can be loaded from this .resx using ResourceManager class.
Although max has provided a very comprehensive answer, I just thought I would add a blog post that I compiled in relation to this question. I offer a solution that uses extension methods on MemberInfo to read the XML comments. My implementation uses XDocument and XPath queries to return the text. It works on methods, properties, types, events, fields and method parameters.
See here: http://www.brad-smith.info/blog/archives/220
You can check a flag in the project options to generate an XML-Documentation.
You can parse the generated file with the usual XML tools.
Depending on the use-case attributes might be a better fit. But in your case it seems to lead to useless redundancy.
But both are a bit problematic with localization. You typically want localizable strings not in the source, but in separate files.
this is not possible because comments do not get compiled
You can't access XML comment informations in your code. As said, these are comments, and are not processed by the compilator.
However they can be used to generate automatic documentation.
If you want to annotate your properties, use Attributes like this one included in the MVC framework

log4net not working

Hey I have this configuration in my web.config
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="mylog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="" />
<param name="Footer" value="" />
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
but log4net is not working. My project compiles fine, and I get no errors debugging either. The lines where I tell to log.debug("somemessage") gets run fine, but I can't find the mylog.log file, so where is it?
One gotcha for this type of thing is to make sure to add the XmlConfigurator attribute to the assembly by placing the following line in your AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator]
Otherwise log4net never activates.
I guess that either log4net is not logging at all, or the file is not ending up where you expect it.
Firstly, have you actually called
XmlConfigurator.Configure()
anywhere in your code? If the xml snippet above is in the application configuration file, this call will do the trick. If the xml snippet is in it's own file, you'll need to use the .Configure(string) overload that takes the path to the file. Without this call (or apparently the assembly level attribute mentioned by Kirk Woll), then log4net won't be logging at all.
If you believe this is all done, and log4net should be logging, then maybe you should put a fully qualified path in for the log file while you debug further. That will let you be sure where the file should be.
There is another small gotcha, see here: http://logging.apache.org/log4net/release/manual/configuration.html#dot-config
the [assembly: log4net.Config.XmlConfigurator] method doesn't work with app.config. If you configure log4net from app.config, you must use the log4net.Config.XmlConfigurator.Configure() method.
Here is my checklist for when log4net is proving to be recalcitrant:
ensure that log4net.config file is copied to the bin\ folder when building (set to 'Copy if newer' in compiler)
when dealing with installed code, ensure that the log4net.config came along for the ride (set to 'Content' in compiler)
ensure that the user that the process runs as has write rights to the folder where the logs are to be written
if in doubt, give promiscuous permissions to c:\temp\ and get everything to log to there ()
fire up Sysinternal/Dbgview.exe to see if that tells you anything
For an ASP.NET MVC project adding
log4net.Config.XmlConfigurator.Configure();
to the Global.asax.cs also helps:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
log4net.Config.XmlConfigurator.Configure();
}
}
These are the steps which eventually got my file logging working:
-Check AssemblyInfo.cs contains the following attribute.
[assembly: log4net.Config.XmlConfigurator]. This loads log4net.
Check the log directory has write permissions.
Check the logger has a format specified. This is done by checking each element in your configuration has a layout element specified. Eg:
<appender name="MainLogger"...
<layout type="log4net.Layout.SimpleLayout"/>
Finally, try turning on log4net internal logging to enable console logging and checking the console. To do this, add <add key="log4net.Internal.Debug" value="true"/> to your appSettings.
I've had experiences where logging systems fail silently without raising exceptions. I suppose this makes sense because if the logger is logging errors then how can it log an error that it's unable to perform logging?
So if the file isn't created on disk, start by looking into file system permissions to ensure the user your application is running under can write a new file to that disk location.
For testing purposes you might want to manually create the file on disk that should be written to and open up permissions for everybody to write to it. If the logger starts writing to it then you know it's permission based rather than configuration based.
On my side, I forgot to mark the config file to be copied while the app is being compiled.
Just right click the log4net.config file, select property and then choose Copy to Output Directory to be Copy XXXX
Unfortunately none of the above helped. Explicit configuration in the class to be logged additionaly to the previous settings suggestions did the trick for me.
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
log4net.Config.XmlConfigurator.Configure(new FileInfo(assemblyFolder + "/log4net.config"));
So, in the Accepted Answer, the solution was to put
[assembly: log4net.Config.XmlConfigurator]
into the AssemblyInfo.cs File.
Also, there was a comment sugesting
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
however, if it is still not working, consider configuring the name of your log4net config file (log4net.config in my case):
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
after that, logging worked like a charm.
I tried all of the above but nothing worked. Adding this line in app.config's configSections section worked for me.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
Ensure that Version and PublicKeyToken is correct
<param name="File" value="mylog.log" />
is saying "write mylog.log to actual folder". This means that if your webapp is under IIS, then log will be written to C:\inetpub\wwwroot\appname\mylog.log.
If log file is not there, then maybe account under which is app running doesn't have write permission on folder. You can run Process Monitor from SysInternals to see if and where a file is written.
Also run VS in debug mode, to see if any exceptions are thrown (Debug->Exceptions->CLR Exceptions, check Thrown).
In my case I forget to set the log4Net.config file properties as "Content" so the file was not included in the deploy.
So pay attention on it:
Compile action : Content
Check your Reporting level is set to "Debug" or "ALL".
This caught me and I spent ages trying to debug my log4net setup.
I had it set to Error and as it wasn't erroring it didn't log anything.
Aside from all the other answers, my problem that was I needed a call to GetLogger() in my start-up class (WinForms example):
static class Program
{
public static readonly ILog MainLog = LogManager.GetLogger("Main");
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
All my 'meaningful' logging statements were in a DLL that is apparently loaded too late for log4net.
We also had problems with log4net not working and in our case the problem was that the config file had Windows line breaks instead of Unix line breaks.
Also after some hours, I figured out why it wasn't working for me...
i had:
public static class Program
{
private static CommunicationManager _bcScanner = new CommunicationManager();
private static ILog _log = LogManager.GetLogger(typeof(Program));
private static SocketServer socketListener;
but it should be:
public static class Program
{
private static ILog _log = LogManager.GetLogger(typeof(Program));
private static CommunicationManager _bcScanner = new CommunicationManager();
private static SocketServer socketListener;
So make sure the ILog is on the first line....

dynamic log4net appender name?

Let's say i have 3 smtp appenders in same log4net file whose names are:
<appender name = "emailDevelopment".. />
<appender name = "emailBeta".. />
<appender name = "emailProduction".. />
Let's say i have 3 different servers(Dev, Beta, Production). Depending upon the server, i want to fire the log. In case of Development server, it would fire log from "emailDevelopment". I have a system variable in each server named "ApplicationEnvironment" whose value is Development, Beta, Production based on the server names. Now is there anyway i can setup root in log4net so that it fires email depending upon the server name.
<root>
<priority value="ALL" />
<appender-ref ref="email<environment name from whose appender should be used>" />
</root>
This doesn't directly answer your question, but another approach is to simply have multiple log4net configuration files and call XmlConfigurator.Configure() on the right one. For example, you might have Logging.Development.Config, Logging.Beta.Config and so on.
Somewhere in code, you determine the "environment" and configure using the file you want.
I've even gone so far as to have multiple config files and pull different parts of them out into a single XML representing the "true" config, and then calling the Configure() method on that. For example, Logging.Appenders.Config which has all the appenders, and takes all of them and combines it with one of your environment-specific config files above; the environment-specific ones simply reference what they need, and the rest are effectively inactive/unreferenced for that environment.
Even after having written the only XSD file for log4net configuration I'm still not aware of an easy way to achieve this.
You might be able to do something like:
log4net.GlobalContext.Properties["host"] = new ClassThatToStringsHost();
class ClassThatToStringsHost
{ public override string ToString() { return "whatever"; } }
Now you can reference this value from the Log format with: "%property{host}"
To perform the filtering you will need to use a filter configuration in the adapter(s):
<appender name="file" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.PropertyFilter">
<Key value="host" />
<StringToMatch value="whatever" />
</filter>
<!-- Anything not accepted by the above should be excluded -->
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
There may even be a built-in property you could leverage and this should work. See also this post: http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx
For me, myself, and I... I would approach it another way all together. I would derive my own SMTP appender from the default and in the ActivateOptions() method I'd configure the values according to the environment. This would allow you to use one SMTP appender with consistent rules and yet provide three public properties for each of the email addresses you want to send from. It's not hard, give it a try!

How can I change the file location programmatically?

I am totally new to Log4net. I have managed to get something going by adding a config file and simple logging. I have hardcoded the value to be "C:\temp\log.txt" but this is not good enough.
The logs must go to the special folders
path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
and this path changes depending whether you are using Windows Server 2008 or Windows XP or Vista etc...
How can I just change the location of the file in log4net programmatically?
This is what I have done:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\temp\log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
class Program
{
protected static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log.Warn("Log something");
path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
// How can I change where I log stuff?
}
}
Just need to figure out how I can change to log stuff to where I want to.
Any suggestions?
Thanks a lot
log4net can handle this for you. Any appender property of type string can be formatted, in this case, using the log4net.Util.PatternString option handler. PatternString even supports the SpecialFolder enum which enables the following elegant config:
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString"
value="%envFolderPath{CommonApplicationData}\\test.txt" />
...
</appender>
Here's a unit test that proofs the pudding:
[Test]
public void Load()
{
XmlConfigurator.Configure();
var fileAppender = LogManager.GetRepository()
.GetAppenders().First(appender => appender is RollingFileAppender);
var expectedFile =
Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData),
"test.txt");
Assert.That(fileAppender,
Is.Not.Null & Has.Property("File").EqualTo(expectedFile));
}
The following test verifies that log4net actually writes to disk (which basically makes this an "integration" test, not a unit test, but we'll leave it at that for now):
[Test]
public void Log4net_WritesToDisk()
{
var expectedFile =
Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData),
"test.txt");
if (File.Exists(expectedFile))
File.Delete(expectedFile);
XmlConfigurator.Configure();
var log = LogManager.GetLogger(typeof (ConfigTest));
log.Info("Message from test");
LogManager.Shutdown();
Assert.That(File.ReadAllText(expectedFile),
Text.Contains("Message from test"));
}
NB: I strongly suggest using the compact property syntax demonstrated in the above sample. Removing all those "<property name=" makes your config that much more readable.
I found a mutation of this code in the interwebs:
XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h =
(log4net.Repository.Hierarchy.Hierarchy) LogManager.GetRepository();
foreach (IAppender a in h.Root.Appenders)
{
if (a is FileAppender)
{
FileAppender fa = (FileAppender)a;
// Programmatically set this to the desired location here
string logFileLocation = #"C:\MySpecialFolder\MyFile.log";
// Uncomment the lines below if you want to retain the base file name
// and change the folder name...
//FileInfo fileInfo = new FileInfo(fa.File);
//logFileLocation = string.Format(#"C:\MySpecialFolder\{0}", fileInfo.Name);
fa.File = logFileLocation;
fa.ActivateOptions();
break;
}
}
This works for me. Our application needs to put the log file in a folder that contains the version number of the app based on the AssemblyInfo.cs file.
You should be able to set the logFileLocation programmatically (e.g. you can use Server.MapPath() if this is a web application) to suit your needs.
Looks like Peter's answer doesn't work for Log4net v1.2.10.0.
An alternative method is described here.
Basically the method is to implement a custom pattern converter for the log4net config file.
First add this class to your project:
public class SpecialFolderPatternConverter : log4net.Util.PatternConverter
{
override protected void Convert(System.IO.TextWriter writer, object state)
{
Environment.SpecialFolder specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), base.Option, true);
writer.Write(Environment.GetFolderPath(specialFolder));
}
}
Then set up the File parameter of your FileAppender as follows:
<file type="log4net.Util.PatternString">
<converter>
<name value="folder" />
<type value="MyAppName.SpecialFolderPatternConverter,MyAppName" />
</converter>
<conversionPattern value="%folder{CommonApplicationData}\\SomeOtherFolder\\log.txt" />
</file>
Basically the %folder tells it to look at the converter called folder which points it to the SpecialFolderPatternConverter class. It then calls Convert on that class, passing in the CommonApplicationData (or whatever) enum value.
How about a simple:
XmlConfigurator.LogFullFilename = #"c:\ProgramData\MyApp\Myapp.log";
Why is it so complex to do a really simple thing?
To also change the error log's path (based on JackAce's answer):
private static void SetLogPath(string path, string errorPath)
{
XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h =
(log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
foreach (var a in h.Root.Appenders)
{
if (a is log4net.Appender.FileAppender)
{
if (a.Name.Equals("LogFileAppender"))
{
log4net.Appender.FileAppender fa = (log4net.Appender.FileAppender)a;
string logFileLocation = path;
fa.File = logFileLocation;
fa.ActivateOptions();
}
else if (a.Name.Equals("ErrorFileAppender"))
{
log4net.Appender.FileAppender fa = (log4net.Appender.FileAppender)a;
string logFileLocation = errorPath;
fa.File = logFileLocation;
fa.ActivateOptions();
}
}
}
}
This worked for me:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
..
<file value="${APPDATA}\MyApp\MyApp Client\logs\Log.txt"/>
..
</log4net>
If needing to write to special folders I found help here (2nd and 3rd example).
Edit:
To answer OP.. This works for 'all users' area:
...
<file value="${ALLUSERSPROFILE}\MyApp\MyApp Client\logs\Log.txt"/>
...
Which is normally "C:\ProgramData" in newer versions of Windows.
See these too:
How to specify common application data folder for log4net? == https://stackoverflow.com/a/1889591/503621 and comments
&
https://superuser.com/q/405097/47628
https://stackoverflow.com/a/5550502/503621
JackAce's answer, just more concise using Linq:
C#
XmlConfigurator.Configure();
var appender = (LogManager.GetRepository() as Hierarchy).Root.Appenders
.OfType<FileAppender>()
.First();
appender.File = logPath;
appender.ActivateOptions();
VB.NET
XmlConfigurator.Configure()
Dim appender = CType(LogManager.GetRepository(), Hierarchy).Root.Appenders _
.OfType(FileAppender)() _
.First()
appender.File = logPath
appender.ActivateOptions()
Great use case for LINQs OfType<T> filter:
/// <summary>
/// Applies a transformation to the filenames of all FileAppenders.
/// </summary>
public static void ChangeLogFile(Func<string,string> transformPath)
{
// iterate over all FileAppenders
foreach (var fileAppender in LogManager.GetRepository().GetAppenders().OfType<FileAppender>())
{
// apply transformation to the filename
fileAppender.File = transformPath(fileAppender.File);
// notify the logging subsystem of the configuration change
fileAppender.ActivateOptions();
}
}
If the filename in the app.config is log.txt this will change the log output to logs/some_name_log.txt:
ChangeLogFile(path => Path.Combine("logs", $"some_name_{Path.GetFileName(path)}"));
To Answer the OPs original problem that would be:
ChangeLogFile(path => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), path));
As an alternative to doing this programatically, you can use environment variables and customizable patterns in the configuration file. See this response to a similar question.
Look at "PatternString for pattern based configuration" in the Log4Net V1.2.10 release notes.
Also if you are thinking of writing to a directory such as Enviroment.SpecialFolder.CommonApplicationData you need to consider:
Will all instances of your application for all users have write access to the log file? E.g. I don't believe non-administrators will be able to write to Enviroment.SpecialFolder.CommonApplicationData.
Contention if multiple instances of your application (for the same or different users) are attempting to the same file. You can use the "minimal locking model" (see http://logging.apache.org/log4net/release/config-examples.html to allow multiple processes to write to the same log file, but there probably will be a performance impact. Or you could give each process a different log file, e.g. by including the process id in the filename using a customizable pattern.
In the current version of Log4Net (2.0.8.0) you could simply use <file value="${ProgramData}\myFolder\LogFiles\" /> for C:\ProgramData\.. and ${LocalAppData} for C:\Users\user\AppData\Local\
If you have to deploy to unknown systems and you want to use the simple solution by Philipp M even with different special folders you can retrieve the special folder path you want and set a custom env variable before loading the log4net configuration.
string localData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Environment.SetEnvironmentVariable("MY_FOLDER_DATA", localData);
XmlConfigurator.Configure( ...
... just to be sure the env variable exists and has the value you want.
Those who are looking to set the file path to Project root dicrectory Path can refer below code. I have implemented this in .NET 5 API Project
Changes for Log4net.config file. Remember type="log4net.Util.PatternString" this line is very important
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LoggerFilePath}\Logs\server.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
Then, in Program.cs file
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context,loggerBuilder) =>
{
log4net.GlobalContext.Properties["LoggerFilePath"] = context.HostingEnvironment.ContentRootPath;
loggerBuilder.ClearProviders();
loggerBuilder.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Thanks!
//Make sure you include System.Linq
//Though is an answer to old question,
//but more concise, just two lines of code.
((FileAppender)LogManager.GetRepository().GetAppenders().Select(a => a).First()).File = #"C:\log.txt";
((FileAppender)LogManager.GetRepository().GetAppenders().Select(a => a).First()).ActivateOptions();

Categories

Resources