關於我自己

2020年7月8日 星期三

NLog




 private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        public ActionResult Index()
        {
            try
            {
                int a = 6;
                int b = 0;
                int result = a / b;
            }
            catch (Exception ex)
            {
                GlobalDiagnosticsContext.Set("username", "Jeff");
                logger.Error(ex, "testLog");

            }
            return View();
        }
==============
<?xml version="1.0" ?>
<nlog autoReload="true">
  <targets>
    <target name="database" type="Database">

      <!-- database connection parameters -->
      <!-- alternatively you could provide a single 'connectionstring' parameter -->

      <dbhost>BabyBig-PC</dbhost>
      <dbdatabase>ch01</dbdatabase>
      <dbusername>xxx</dbusername>
      <dbpassword>xxx</dbpassword>

      <commandText>
        insert into dbo.NLog (
        Application, Logged, Level, Message,
        Username,
        ServerName, Port, Url, Https,
        ServerAddress, RemoteAddress,
        Logger,Callsite, Exception
        ) values (
        @Application, @Logged, @Level, @Message,
        @Username,
        @ServerName, @Port, @Url, @Https,
        @ServerAddress, @RemoteAddress,
        @Logger, @Callsite, @Exception
        );
      </commandText>
      <parameter name="@application" layout="${appsetting:name=AppName:default=Unknown}" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@username" layout="${gdc:username}" />
      <parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
      <parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
      <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
      <parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
      <parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
      <parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite}" />
      <parameter name="@exception" layout="${exception:tostring}" />
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Warn" writeTo="database" />
  </rules>

</nlog>



沒有留言:

張貼留言