NLog xsi:type not working with custom target
Asked Answered
V

2

5

I wanted to write custom target in NLog using this: https://github.com/nlog/nlog/wiki/How%20to%20write%20a%20Target

and write my logs to MongoDB, so my code looks like this:

namespace NLog.Mongo
{
    [Target("Mongo")]
    public sealed class MongoDBNLogTarget : Target
    {
        ...
        protected override void Write(NLog.LogEventInfo logEvent)
        {
            Repository.Insert(logEvent);
        }
    }
}

and I imagine my NLog.config file should look like this:

<?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.Mongo"/>
  </extensions>
  <targets>
    <target name="mongo" xsi:type="Mongo"/>
  </targets>

  <rules>
    <logger name="*" minLevel="Info" writeTo="mongo" />
  </rules>
</nlog>

However I get warning:

This is an invalid xsi:type 'http://www.nlog-project.org/schemas/NLog.xsd:Mongo'
Varanasi answered 21/11, 2014 at 16:1 Comment(0)
K
5

This is a error from the XSD, which should be seen as warning. The XSD is generated with all the possible targets (in the NLog main package) and thus doesn't have the custom targets.

These kind of errors could be ignored and NLog won't stop working if the XML config contains these kind of "errors".

Karalee answered 3/9, 2016 at 21:0 Comment(0)
C
-1

I believe we need to extend the existing Nlog.xsd schema.

Taking advice from (How do I extend a base schema with custom elements while remaining open to change from new versions?)

This way we could support extended types in the system.

Convolvulus answered 14/6, 2024 at 15:16 Comment(1)
Aditya Gupta, a link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Introrse

© 2022 - 2025 — McMap. All rights reserved.