2
votes

I am using NLog.web and Nlog.config . Able to log error to file . When try to log to database. Getting target database cant be found.

Please help if any one using Nlog with asp.net core rc1 and logging error to database

My configuration look likes

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="Nlog.log">
  <targets>
    <target name="file" xsi:type="File" layout="${longdate}|${level}|${callsite}|${logger}|${threadid}|${windows-identity:domain=false}--${message} ${exception:format=message,stacktrace:separator=*" fileName="c:\psnet\myapplication.log"/>
    <target name="database" type="Database">
      <connectionString>
        Data Source=databaseservername;Initial Catalog=databasename;User Id=username;Password=password;
      </connectionString>
      <commandText>
        insert into system_logging(log_date,log_level,log_logger,log_message,log_machine_name, log_user_name, log_call_site, log_thread, log_exception, log_stacktrace) values(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
      </commandText>
      <parameter name="@time_stamp" layout="${longdate}"/>
      <parameter name="@level" layout="${level}"/>
      <parameter name="@logger" layout="${logger}"/>
      <parameter name="@message" layout="${message}"/>
      <parameter name="@machinename" layout="${machinename}"/>
      <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
      <parameter name="@call_site" layout="${callsite:filename=true}"/>
      <parameter name="@threadid" layout="${threadid}"/>
      <parameter name="@log_exception" layout="${exception}"/>
      <parameter name="@stacktrace" layout="${stacktrace}"/>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="file"/>
    <logger name="*" minlevel="Info" appendTo="database"/>
  </rules>
</nlog>

I am able to write to file.. IN Nog internal file it showing error target database is unkown Even when trying to write to event log showing same error Target EventLog is unknown

Please help me to fix this issue

1
Your configuration will probably be wrong but we can't tell because you're not showing us anything.cverb
I Have added complete configuration .Please have a lookRahul Sharma
EventLog on NetCore requires: nuget.org/packages/NLog.WindowsEventLog . DatabaseTarget on NetCore requires: nuget.org/packages/System.Data.SqlClientRolf Kristensen

1 Answers

3
votes

You need as the NLog.Extensions.Logging package

1. Add dependency in project.json

"dependencies": {
    "NLog.Extensions.Logging": "1.0.0-*"
 }

2. Create nlog.config in root of your project file, see NLog.config example

3. in startup.cs add in Configure

  using NLog.Extensions.Logging;

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
      //add NLog to ASP.NET Core
      loggerFactory.AddNLog();


      //needed for non-NETSTANDARD platforms: configure nlog.config in your project root
      env.ConfigureNLog("nlog.config");
      ...

4. Include NLog.config for publishing in project.json:

   "publishOptions": {
        "include": [
            "wwwroot",
            "Views",
            "appsettings.json",
            "web.config",
            "nlog.config"
        ]
    },

5. Not working?

Enabled and check the internal log!