Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How do I configure log4net from an arbitrary data structure?

My assumption is that, internally, log4net loads its XML file into some data structure (maybe not a dict, but some equivalent of cfg_dict above) and then passes that structure to whatever code ma...

posted 3y ago by Peter Taylor‭  ·  edited 3y ago by Peter Taylor‭

Answer
#2: Post edited by user avatar Peter Taylor‭ · 2021-03-24T20:37:36Z (about 3 years ago)
Complete rewrite in light of changes to the question
  • The [documentation](https://logging.apache.org/log4net/release/manual/configuration.html) gives an example using `XmlConfigurator`:
  • > using log4net;
  • > using log4net.Config;
  • >
  • > public class MyApp
  • > {
  • > private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
  • >
  • > static void Main(string[] args)
  • > {
  • > // BasicConfigurator replaced with XmlConfigurator.
  • > XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));
  • >
  • > log.Info("Entering application.");
  • > Bar bar = new Bar();
  • > bar.DoIt();
  • > log.Info("Exiting application.");
  • > }
  • > }
  • In this example the filename is passed from the command line, but you can easily replace that.
  • If what you dislike is specifically the XML, there's probably some way of mashing it up with [Newtonsoft's JSON.Net](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) to turn a JSON file into an `XElement` and pass that to the alternative `XmlConfigurator.Configure` method.
  • > My assumption is that, internally, log4net loads its XML file into some data structure (maybe not a dict, but some equivalent of cfg_dict above) and then passes that structure to whatever code manages the configuration (the equivalent of dictConfig above).
  • What's idiomatic in duck-typed Python is not always idiomatic in statically typed C#. Using untyped `Dictionary` in C# is not really considered good style in code which postdates `System.Collections.Generic`, and using dictionaries which mixed value types was never really good style.
  • So, yes, log4net loads its XML file into a data structure, but that data structure is `System.Xml.Linq.XElement`. If you construct your own `XElement` then you can pass it to `XmlConfigurator.Configure`.
  • [Newtonsoft's JSON.Net](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) has some support for parsing a JSON file into an `XElement`, but I couldn't tell you how straightforward it is to write suitable JSON.
  • Serilog seems to have [built-in support for JSON configuration](https://github.com/serilog/serilog-settings-configuration).
#1: Initial revision by user avatar Peter Taylor‭ · 2021-03-23T18:45:39Z (about 3 years ago)
The [documentation](https://logging.apache.org/log4net/release/manual/configuration.html) gives an example using `XmlConfigurator`:


>     using log4net;
>     using log4net.Config;
>
>     public class MyApp 
>     {
>         private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
>
>         static void Main(string[] args) 
>         {
>             // BasicConfigurator replaced with XmlConfigurator.
>             XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));
>
>             log.Info("Entering application.");
>             Bar bar = new Bar();
>             bar.DoIt();
>             log.Info("Exiting application.");
>         }
>     }

In this example the filename is passed from the command line, but you can easily replace that.

If what you dislike is specifically the XML, there's probably some way of mashing it up with [Newtonsoft's JSON.Net](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) to turn a JSON file into an `XElement` and pass that to the alternative `XmlConfigurator.Configure` method.