在自定义NLog目标上添加智能感知

时间:2017-03-29 08:24:52

标签: xsd nlog

我希望将第三方目标类型添加到NLog.xsd。您通常会创建一个nlog.config文件,如下所示:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ...>
  ...
  <targets>
    <target name="file" xsi:type="File" />
  </targets>
  ...
</nlog>

现在我想添加一个新目标:

<targets>
  <target name="file" xsi:type="File" />
  <target name="file" xsi:type="MyTarget" />
</targets>

XSD验证在Visual Studio中发出警告,因为MyTarget未在任何地方定义。我尝试将MyTarget添加到NLog.xsd并引用更新的NLog.xsd,但之后我没有得到任何智能感知:

<nlog xmlns="http://my-project.com/NLog.xsd" ...>

有任何想法如何实现这个?

1 个答案:

答案 0 :(得分:1)

自定义Syslog目标(NLog.Targets.Syslog)创建了一个扩展NLog XSD的自定义XSD。

内容:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NLog.Targets.Syslog"
           elementFormDefault="qualified"
           targetNamespace="http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:sl="http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd"
           xmlns:nlog="http://www.nlog-project.org/schemas/NLog.xsd">
    <xs:import namespace="http://www.nlog-project.org/schemas/NLog.xsd" />
    <xs:complexType name="Syslog">
        <xs:complexContent>
            <xs:extension base="nlog:Target">
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1" />
                    <xs:element name="layout" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                    <xs:element name="enforcement" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="throttling" minOccurs="0" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                            <xs:element name="limit" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="strategy" type="xs:string" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="delay" type="xs:decimal" minOccurs="0" maxOccurs="1" />
                                        </xs:choice>
                                        <xs:attribute name="limit" type="xs:integer" />
                                        <xs:attribute name="strategy" type="xs:string" />
                                        <xs:attribute name="delay" type="xs:decimal" />
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="messageProcessors" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                <xs:element name="splitOnNewLine" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                <xs:element name="transliterate" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                <xs:element name="replaceInvalidCharacters" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                <xs:element name="truncateFieldsToMaxLength" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                <xs:element name="truncateMessageTo" type="xs:integer" minOccurs="0" maxOccurs="1" />
                            </xs:choice>
                            <xs:attribute name="messageProcessors" type="xs:integer" />
                            <xs:attribute name="splitOnNewLine" type="xs:boolean" />
                            <xs:attribute name="transliterate" type="xs:boolean" />
                            <xs:attribute name="replaceInvalidCharacters" type="xs:boolean" />
                            <xs:attribute name="truncateFieldsToMaxLength" type="xs:boolean" />
                            <xs:attribute name="truncateMessageTo" type="xs:integer" />
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="messageCreation" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="facility" type="xs:string" minOccurs="0" maxOccurs="1" />
                                <xs:element name="rfc" type="xs:string" minOccurs="0" maxOccurs="1" />
                                <xs:element name="rfc3164" minOccurs="0" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                            <xs:element name="hostname" type="xs:string" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="tag" type="xs:string" minOccurs="0" maxOccurs="1" />
                                        </xs:choice>
                                        <xs:attribute name="hostname" type="xs:string" />
                                        <xs:attribute name="tag" type="xs:string" />
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="rfc5424" minOccurs="0" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                            <xs:element name="hostname" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="appName" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="procId" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="msgId" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="structuredData" minOccurs="0" maxOccurs="1">
                                                <xs:complexType>
                                                    <xs:choice minOccurs="0" maxOccurs="1">
                                                        <xs:element name="fromEventProperties" type="nlog:Layout" minOccurs="0" maxOccurs="1" />
                                                        <xs:element name="sdElement" minOccurs="1" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:sequence>
                                                                    <xs:element name="sdParam" minOccurs="1" maxOccurs="unbounded">
                                                                        <xs:complexType>
                                                                            <xs:simpleContent>
                                                                                <xs:extension base="xs:string">
                                                                                    <xs:attribute name="name" type="xs:string" use="required" />
                                                                                    <xs:attribute name="value" type="xs:string" use="required" />
                                                                                </xs:extension>
                                                                            </xs:simpleContent>
                                                                        </xs:complexType>
                                                                    </xs:element>
                                                                </xs:sequence>
                                                                <xs:attribute name="sdId" type="xs:string" use="required" />
                                                            </xs:complexType>
                                                        </xs:element>
                                                    </xs:choice>
                                                </xs:complexType>
                                            </xs:element>
                                            <xs:element name="disableBom" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                        </xs:choice>
                                        <xs:attribute name="hostname" type="nlog:SimpleLayoutAttribute" />
                                        <xs:attribute name="appName" type="nlog:SimpleLayoutAttribute" />
                                        <xs:attribute name="procId" type="nlog:SimpleLayoutAttribute" />
                                        <xs:attribute name="msgId" type="nlog:SimpleLayoutAttribute" />
                                        <xs:attribute name="disableBom" type="xs:boolean" />
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="messageSend" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="protocol" type="xs:string" minOccurs="0" maxOccurs="1" />
                                <xs:element name="udp" minOccurs="0" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                            <xs:element name="server" type="xs:string" minOccurs="1" maxOccurs="1" />
                                            <xs:element name="port" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                        </xs:choice>
                                        <xs:attribute name="server" type="xs:string" />
                                        <xs:attribute name="port" type="xs:integer" />
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="tcp" minOccurs="0" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                            <xs:element name="server" type="xs:string" minOccurs="1" maxOccurs="1" />
                                            <xs:element name="port" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="keepAlive" minOccurs="0" maxOccurs="1">
                                                <xs:complexType>
                                                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                                                        <xs:element name="enabled" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                                        <xs:element name="timeout" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                                        <xs:element name="interval" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                                    </xs:choice>
                                                    <xs:attribute name="enabled" type="xs:boolean" />
                                                    <xs:attribute name="timeout" type="xs:integer" />
                                                    <xs:attribute name="interval" type="xs:integer" />
                                                </xs:complexType>
                                            </xs:element>
                                            <xs:element name="reconnectInterval" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="useTls" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="framing" type="xs:string" minOccurs="0" maxOccurs="1" />
                                            <xs:element name="dataChunkSize" type="xs:integer" minOccurs="0" maxOccurs="1" />
                                        </xs:choice>
                                        <xs:attribute name="server" type="xs:string" />
                                        <xs:attribute name="port" type="xs:integer" />
                                        <xs:attribute name="reconnectInterval" type="xs:integer" />
                                        <xs:attribute name="useTls" type="xs:boolean" />
                                        <xs:attribute name="framing" type="xs:string" />
                                        <xs:attribute name="dataChunkSize" type="xs:integer" />
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
                <xs:attribute name="name" type="xs:string" />
                <xs:attribute name="layout" type="nlog:SimpleLayoutAttribute" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

用法:(此内容发布于http://nlog-project.org/

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:sl="http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd">

<target xsi:type="sl:Syslog" name="syslog3164-tgt">
而不是

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

另见https://github.com/NLog/NLog.github.io/pull/56

更新:Visual Studio也需要这个:

<nlog xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd"  .. >