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

Dashboard
Notifications
Mark all as read
Q&A

How do I configure log4net from an arbitrary data structure?

+2
−0

I'm used to working in Python, but my current project is in C#/.NET and uses log4net for logging. Out of the box, log4net uses an XML file for configuration. I dislike XML and want to use something else -- possibly JSON or YAML.

In Python I would do this by loading a dictionary with certain keys from whatever external file I feel like (typically json or yaml), and passing it to logging.dictConfig, like this:

# logging.yaml
root:
  level: INFO
  handlers: [file]
handlers:
  file:
    filename: /var/log/appname/appname.log
...more...

# appname.py
with open('/etc/appname/logging.yaml') as f:
    cfg_dict = yaml.load(f.read())
logging.config.dictConfig(cfg_dict)

Note how the file-loading step is orthogonal to the logging-configuration step; this example uses a yaml file, but it could just as easily have been json.

How would I do the equivalent for log4net? 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).

If I know what "that structure" is, and what function expects to receive it, then I can separate the two steps and build it myself from whatever file format I want. If that's not possible (or not simple), is there an alternative logging library that does support this?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comments

It is not clear what the exact problem is here. This shows that it is possible to read the configuration from a file. Indeed this is a structured file (XML), not an "arbitrary data structure". Do you mean something like an YAML configuration file? Providing an actual example would help understand the actual issue. Alexei‭ 18 days ago

YAML is my favored format, yes, but an ideal solution would be file-format-agnostic. E.g. python's dictconfig doesn't care what kind of file (if any) your input dict came from, as long as it has the right keys. ajv‭ 18 days ago

Edited for clarity and I'll add an example in a moment. ajv‭ 18 days ago

1 answer

+3
−0

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 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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment

Thanks. Not quite the answer I wanted, but exactly what I asked for. ajv‭ 10 days ago

Sign up to answer this question »