Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core 2.2 Logging with NLog #17

Open
nagaraju123 opened this issue Jul 6, 2019 · 0 comments
Open

ASP.NET Core 2.2 Logging with NLog #17

nagaraju123 opened this issue Jul 6, 2019 · 0 comments

Comments

@nagaraju123
Copy link

My logs are not inserted into database.But logs are stored in file succesfully.
Here is my 'nlog.cofig"


<?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">
  <targets>
    <target xsi:type="File" name ="allfile" fileName="D:\Playground\asp_net\AspdotnetCore\internal-nlog.txt"/>
         <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="NLog.Appsettings.Standard"/>
  </extensions>
    <target name="database" xsi:type="Database" keepConnection="true" useTransactions="true"
             dbProvider="System.Data.SqlClient" 
             connectionString="data source=localhost;initial catalog=EmployeeDB;integrated security=false;persist security info=True;Trusted_Connection=True;" 
              commandText="INSERT INTO Logs(EventDateTime, EventLevel, UserName, MachineName, EventMessage, Filename, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (@EventDateTime, @EventLevel, @UserName, @MachineName, @EventMessage, @Filename, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage)">    
      <parameter name="@EventDateTime" layout="${date:s}" />
      <parameter name="@EventLevel" layout="${level}" />
      <parameter name="@UserName" layout="${identity}" />
      <parameter name="@MachineName" layout="${machinename}" />
      <parameter name="@EventMessage" layout="${message}" />
      <parameter name="@Filename" layout="${callsite:filename=true}" />
      <parameter name="@ErrorClass" layout="${callsite:class=true}" />
      <parameter name="@ErrorMethod" layout="${callsite:method=true}" />
      <parameter name="@ErrorMessage" layout="${exception}" />
      <parameter name="@InnerErrorMessage" layout="${stacktrace}" />
    </target>
  </targets>
  <rules>
      <logger name="database" minlevel="Trace"  writeTo="database" />
    <logger name ="file" minlevel="Trace" writeTo="allfile"></logger>
  </rules>
</nlog>

My Controller where i am calling this logger:-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspdotnetCore.cars;
using AspdotnetCore.YTBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;

namespace AspdotnetCore.controllers
{
public class HomeController
{
private static Logger logger = LogManager.GetLogger("file"); // For file
private static Logger log=LogManager.GetLogger("database"); // For Name

public HomeController()
{
}
public string Index()
{
logger.Warn("inside file logger from logger"); // It is writing file successfully
log.Warn("inside index from log"); // It is not inserted into database table.
return "In index method";
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant