【问题标题】:NLog with Postsharp not logging带有 Postsharp 的 NLog 不记录
【发布时间】:2017-07-05 14:35:16
【问题描述】:

我发现了问题所在。代码没有问题。我在查询中写的列名不正确的问题。我不知道我是怎么错过这样的事情的,但是感谢您的所有帮助。如果需要,您可以使用 NLog 的代码。

(我尝试使用 NLog 将日志记录到 AOP。我使用 Winform 并使用 Postsharp。将 NLog 代码写入 ExceptionLoggingAttribute.cs。此代码在 Form1.cs 中学习,但在 Aspect 中不起作用。请帮助我!)

AOP 内部

namespace LogLibrary
{
    [PSerializable]
    public class ExceptionLoggingAttribute : OnExceptionAspect
    {
        public FlowBehavior flowbehavior { get; set; }
        public override void OnException(MethodExecutionArgs args)
        {
            NLog.Logger logger = LogManager.GetLogger("databaseLogger");
            logger.Error(args.Exception.Message);
            logger.Fatal(args.Exception.Message);
            logger.Debug(args.Exception.Message);
            logger.Trace(args.Exception.Message);
            args.FlowBehavior = FlowBehavior.Continue;
        }
  }

Nlog.Config 内部

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">


  <variable name="myvar" value="myvalue"/>


  <targets>

    <target name="database" xsi:type="Database"

      connectionStringName="NLogConn"

      commandText="exec dbo.dlog @username,@ErrorName,@MethodName,@StackName, @Date_Time">

      <parameter name="@username" layout="${identity}"/>
      <parameter name="@ErrorName" layout="${message}"/>
      <parameter name="@MethodName" layout="${machinename}"/>
      <parameter name="@StackName" layout="${stacktrace}"/>
      <parameter name="@Date_Time" layout="${date}"/>

    </target>

  </targets>

  <rules>
    <logger name="databaseLogger" minlevel="Trace" writeTo="database" />
  </rules>
</nlog>

App.Config 内部

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="NLogConn" connectionString="Data Source=DMGM0349997\MSSQLSERVER01; Initial Catalog=deneme; Integrated Security=true;"  providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Form1 里面

using System.Windows.Forms;
using PostSharp.Aspects;
using NLog;
using LogLibrary;


namespace LogApp
{
    [ExceptionLogging]
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            throw new Exception("bla bla");
        }
    }
}

希望这张照片能帮助你了解我

【问题讨论】:

  • 你的代码输入aspect code了吗?
  • 可以,但不起作用。
  • 请解释“不起作用”。你在期待什么?目前的结果是什么?
  • 我想在程序失败时将错误信息保存在数据库中。但是这个过程必须在方面
  • 您是否验证了您的内部日志:c:\temp\nlog-internal.log,以查看 NLog 正在尝试做什么。它是一种详细的日志记录,包含有关 NLog 正在做什么的所有信息。所以最好看看那个。它还将记录 NLog 尝试记录某些内容时发生的任何异常

标签: c# aop nlog postsharp


【解决方案1】:

您必须先启用内部日志,在“信息”上设置internalLogLevel:

internalLogLevel="Info" internalLogFile="c:\temp\nlog-internal.log"

如果仍然没有 internalLogFile,那么 NLog 就找不到你的 nlog.config。检查 nlog.config 的位置。

或者你读它的配置是这样的:

LogManager.Configuration = new XmlLoggingConfiguration("pathTo_Nlog.config")

或者在 C# 上设置配置

【讨论】:

  • 我的应用程序文件中有 nlog.config,但我没有 nlog-internal.log 文件。 NLog 也适用于 Form1.cs,但不适用于 Aspect 代码。 (NLog 附加方面库)
  • 我发现了问题所在。谢谢
【解决方案2】:

希望这张照片能帮助你了解我

【讨论】:

  • 请编辑您的问题,而不是在答案中提供信息
  • 你能删除这个“答案”吗?我在问题中为您嵌入了照片。请接受我的编辑 :-)
【解决方案3】:

我遇到了同样的问题。当我执行包含以下代码的 C# 控制台应用程序的主要方法时,没有创建任何日志:

private static readonly ILog Log = LogManager.GetLogger<Program>();

Log.Info("Application Started . . .");

我的问题是 NLog.config 文件的位置。

我在 Visual Studio 中右键单击并选择了 Properties NLog.config。然后对于“复制到输出目录”属性,选择“Copy if newer”选项。

问题是 NLog.config 文件不在我的应用程序的执行目录中。

page 描述了如何通过 XML 规范配置 NLog。 特别阅读日志位置部分。

我的应用配置:

<sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
<common>
    <logging>
      <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog444">
        <arg key="configType" value="INLINE"/>
      </factoryAdapter>
    </logging>
  </common>

和 NLog.config 文件:

<?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"
       internalLogLevel="Trace" internalLogFile="d:\NLog_Internallog.txt">
  <variable name="logDirectory" value="logs" />
  <targets>
    <target xsi:type="File"
         name="fileXmlName"
         header="&lt;nlog&gt;"
         footer="&lt;/nlog&gt;"
         fileName="${logDirectory}/${shortdate}.log.xml"
         concurrentWrites="true"
         createDirs="true"
         autoFlush="true">
      <layout xsi:type="Log4JXmlEventLayout">
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="fileXmlName" />
  </rules>
</nlog>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多