简体   繁体   中英

Custom logging with NLog on database

I have some problem with NLog. According to here that said

If you want custom layout properties (NLog calls them layout renderers) you can use the EventProperties Layout Renderer.

I wrote some config:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="on" internalLogFile="c:\temp\nlog-internal.log">

<targets>
  <!-- database target  -->
  <target name="databaseauthentication"
          xsi:type="Database"
          connectionString="Data Source = [....]; Initial Catalog = [....]; User Id = [....]; Password = [....]"
          commandText="exec dbo.InsertAuthentication
                        @company,
                        @firstname,
                        @lastname,
                        @ip,
                        @pcname,
                        @additionalInfo">
    <parameter name="@company" layout="${event-properties:item=companyValue}" />
    <parameter name="@firstname" layout="${event-properties:item=firstnameValue}" />
    <parameter name="@lastname" layout="${event-properties:item=lastnameValue}" />
    <parameter name="@ip" layout="${event-properties:item=ipValue}" />
    <parameter name="@pcname" layout="${event-properties:item=pcnameValue}" />
    <parameter name="@additionalInfo" layout="${event-properties:item=additionalInfoValue}" />
  </target>
</targets>


<rules>
   <logger levels="Info" name="asyncdatabaseauthenticationLogger" writeTo="asyncdatabaseauthentication"/>
   <logger levels="Info" name="databaseauthenticationLogger" writeTo="databaseauthentication"/>
</rules>
</nlog>

And used that like:

 public static void SendLogin()
        {

            var eventInfo = new LogEventInfo(LogLevel.Info, databaseAuthenticateLogger.Name, "Message");
            eventInfo.Properties["firstnameValue"] = "My Fist Name;
            eventInfo.Properties["lastnameValue"] = "My Last Name";
            eventInfo.Properties["companyValue"] = "My Company";
            eventInfo.Properties["ipValue"] = "IP";
            eventInfo.Properties["pcnameValue"] = "PC Name";
            eventInfo.Properties["additionalInfoValue"] = "Login";
            databaseAuthenticateLogger.Log(eventInfo);
        }

But this code don't work. Can anyone tell me where is my mistake?

The Above code is true, But We need to add this part in c# code:

static Logger databaseAuthenticateLogger = LogManager.GetLogger("databaseauthenticationLogger");
static Logger asyncdatabaseAuthenticateLogger = LogManager.GetLogger("asyncdatabaseauthenticationLogger");

Everythings works good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM