DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Store Log4Net Logs in MongoDB

How to Store Log4Net Logs in MongoDB

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
May. 05, 12 · Interview
Like (0)
Save
Tweet
Share
9.08K Views

Join the DZone community and get the full member experience.

Join For Free

one of the most simple and useful way to introduce a documents database like mongo in your organization is to use as log storage. if you use log4net you can download a mongo appender capable of storing logs inside mongo with few lines of code and you are ready to go.

the original appender is really good but i’ve done a little modification to make it capable of storing complex item in extended properties, just locate the function loggineventtobson and modify the section that stores composite properties with following code

if (compositeproperties != null && compositeproperties.count > 0)
{
    var properties = new bsondocument();
    foreach (dictionaryentry entry in compositeproperties)
    {
        bsonvalue value;
        if (!bsontypemapper.trymaptobsonvalue(entry.value, out value))
        {
            properties[entry.key.tostring()] = entry.value.tobsondocument();
        }
        else
        {
            properties[entry.key.tostring()] = value;
        }
    }
    toreturn["customproperties"] = properties;
}

the only modification i did to the original code is trying to convert the value stored in composite properties to a bsonvalue and if the conversion does not succeeds i convert the entire object to a bsondocument and store the entire object to mongo . the original code stores extended properties with tostring(), this has two disadvantages, the first is that you are actually losing the type of the data and you are not able to store complex objects. with this modification if you add an int32 as extended property, it will be stored as numeric value inside the database.

using mongo as log storage gives you lots of advantages, first of all document databases are really fast in inserting stuff, then they are not schema bound so you do not need to define a schema to contain your logs. this feature in conjunction with the ability of log4net to store extended properties in the context permits you to write code like this one.

log4net.globalcontext.properties["currentitem"] = item;

with this simple line of code each subsequent log will have in mongo database an associated object of type item, until you will not remove the object from the globalcontext.

complex objects are stored inside the log

figure 1: your log contains all data from extended properties

as you can see from figure 1 the log has now a complex object associated with it. this capability is awesome because you simply need to store complex objects inside globalcontext and they will be stored inside the log as customproperties without needing additional lines of code.

clearly you can now use all the advanced query capability of mongo to view/filter/mapreduce logs using these additional properties.

Database Property (programming) MongoDB

Published at DZone with permission of Ricci Gian Maria. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Comparing Map.of() and New HashMap() in Java
  • What Is JavaScript Slice? Practical Examples and Guide
  • Distributed Tracing: A Full Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: