Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:13:29+00:00 2026-06-10T19:13:29+00:00

I’d like to use NLog to log to a RavenDb database. There is apparently

  • 0

I’d like to use NLog to log to a RavenDb database. There is apparently no existing NLog Target for this (according to this, and searches), so I’m attempting to write my own. I keep getting in conflict with the RavenDb client’s own NLog config, and I can’t work out how to fix this. Here’s my Target:

namespace NLog.RavenDb
{
    class LogEntry
    {
        public string Id { get; set; }
        public Exception Exception { get; set; }
        public string LogLevel { get; set; }
        public StackTrace StackTrace { get; set; }
        public DateTimeOffset TimeStamp { get; set; }
        public string Message { get; set; }
    }

    public class RavenDbTarget : NLog.Targets.TargetWithLayout
    {
        public static IDocumentStore Store { get; set; }

        protected override void Write(LogEventInfo logEvent)
        {
            if (Store == null)
            {
                const string noStoreWarning = "No Document Store set for the RavenDb Log target";
                Debug.WriteLine(noStoreWarning);
                return;
            }

            LogEntry entry = new LogEntry
            {
                Exception = logEvent.Exception,
                LogLevel = logEvent.Level.Name,
                Message = logEvent.FormattedMessage,
                StackTrace = logEvent.StackTrace,
                TimeStamp = new DateTimeOffset(logEvent.TimeStamp)
            };

            using (var session = Store.OpenSession())
            {
                session.Store(entry);
                session.SaveChanges();
            }
        }
    }
}

And here’s how I can get it to work with a SimpleConfigurator:

RavenDbTarget.Store = new DocumentStore { Url = "http://localhost:8080/" };
RavenDbTarget.Store.Initialize();

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(new RavenDbTarget());
Logger logger = NLog.LogManager.GetLogger("Any");
logger.Info("Hi");

However, NLog.config is used by RavenDb too, so when I put references to my Target in there , it throws an exception at Store.Initialize(). Here’s what I tried in NLog.config (NLog.RavenDb is the name of my library project containing the RavenDb Target):

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <extensions>
        <add assembly="Nlog.RavenDb"/>
    </extensions>

    <targets>
        <target xsi:type="NLog.RavenDb.RavenDbTarget" name="Raven"/>
        <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate} ${uppercase:${level}} ${message}" />
    </targets>

    <rules>
        <logger minlevel="Trace" name="Any" writeTo="Raven" />
    </rules>
</nlog>

How can I make my NLog.config file play nice with RavenDb’s use of it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:13:31+00:00Added an answer on June 10, 2026 at 7:13 pm

    Found it. I missed the Target attribute on my target class as shown here.

    [NLog.Targets.Target("RavenDb")]
    public class RavenDbTarget : NLog.Targets.TargetWithLayout
    

    The name parameter you pass to the Target attribute is the one you reference as “type” in your config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        <extensions>
            <add assembly="Nlog.RavenDb"/>
        </extensions>
    
        <targets>
            <target xsi:type="RavenDb" name="Raven"/>
        </targets>
    
        <rules>
            <logger minlevel="Trace" name="Any" writeTo="Raven" />
        </rules>
    </nlog>
    

    Now NLog picks up NLog.config automatically, and I don’t need any extra config code. Here’s the contents of my test console app’s main method now:

            RavenDbTarget.Store = new DocumentStore { Url = "http://localhost:8080/" };
            RavenDbTarget.Store.Initialize();
    
            Logger logger = LogManager.GetLogger("Any");
            logger.Info("Hi");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.