Nlog直接访问有效,但PostSharp默认不可用

问题描述 投票:0回答:1

我是关于PostSharp的工作原理的新手。我已经使用NLog编写了一些代码,希望使用PostSharp进行改进。

  • Nlog输出正在工作(在控制台和文件上都完成了输出。] >>
  • PostSharp根本不起作用(没有输出到文件,没有输出控制台)。>>
  • 我试图遵循PostSharp网站上的文档:https://doc.postsharp.net/nlog

    我不知道我的错误在哪里:(

有人可以帮我解决这个问题吗?

感谢您的帮助。

using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.NLog;
using NLog.Config;
using NLog.Targets;
using System.Text;
using NLog;
using LogLevel = PostSharp.Patterns.Diagnostics.LogLevel;
using static PostSharp.Patterns.Diagnostics.FormattedMessageBuilder;

namespace MyNameSpace
{
    static class LogSources
    {
        // Configure a prototype LogSource that you will reuse in several classes.
        public static readonly LogSource Default = LogSource.Get().WithLevels(LogLevel.Trace, LogLevel.Warning);
    }

    /// <summary>
/// Main class
/// </summary>
[Log]
class Program 
{
    static LoggingConfiguration config;
    static readonly LogSource logSource = LogSource.Get().WithLevels(LogLevel.Trace, LogLevel.Warning);
    public static ProgramOptions Options { get; private set; }

    /// <summary>
    /// MAIN ENTRY
    /// </summary>
    static void Main()
    {
        InitNLog(Environment.ExpandEnvironmentVariables (@"%TEMP%\MyLog.txt"));
        InitPostSharp();

        LogMe();

        logSource.Default.Write (Formatted("This is a TEST message"));
        NLog.LogManager.GetCurrentClassLogger().Trace("This is a Trace message");
        NLog.LogManager.GetCurrentClassLogger().Debug("This is a Debug message");
        NLog.LogManager.GetCurrentClassLogger().Info("This is an Info message");
        NLog.LogManager.GetCurrentClassLogger().Warn("This is a Warn message");
        NLog.LogManager.GetCurrentClassLogger().Error("This is an Error message");
        NLog.LogManager.GetCurrentClassLogger().Fatal("This is a Fatal error message");

        Console.ReadKey();
    }

    [Log]
    public static void LogMe()
    {
        logSource.Default.Write(Formatted("pleassseee loggggg meeeee"));
    }

    /// <summary>
    /// Initializes PostSharp.
    /// </summary>
    private static void InitPostSharp()
    {
        LoggingServices.DefaultBackend = new NLogLoggingBackend(new LogFactory(config));

    }

    /// <summary>
    /// Init NLog system
    /// </summary>
    /// <param name="outputFileName">Full path for output logging file.</param>
    static void InitNLog(string outputFileName)
    {
        // NLOG CONFIGURATION - https://github.com/NLog/NLog/wiki/Tutorial
        // Step 1. Create configuration object 
        config = new LoggingConfiguration();

        // Step 2. Create targets and add them to the configuration 
        ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();

        consoleTarget.Layout = @"${date:universalTime=true:format=HH\:mm\:ss.fff} ${message} ${exception:format=Message}";
        consoleTarget.UseDefaultRowHighlightingRules = false;
        consoleTarget.RowHighlightingRules.Clear();
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Trace and starts-with('${message}','[THREAD:')", ConsoleOutputColor.Cyan, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Trace", ConsoleOutputColor.DarkCyan, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Debug", ConsoleOutputColor.DarkGray, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Info", ConsoleOutputColor.White, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Warn", ConsoleOutputColor.Yellow, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Error", ConsoleOutputColor.Red, ConsoleOutputColor.Black));
        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level >= LogLevel.Fatal", ConsoleOutputColor.White, ConsoleOutputColor.DarkRed));
        consoleTarget.Encoding = Encoding.Unicode;
        config.AddTarget("console", consoleTarget);

        FileTarget fileTarget = new FileTarget();
        config.AddTarget("file", fileTarget);
        fileTarget.FileName = outputFileName;

        // Step 3. Set target properties 
        consoleTarget.Layout = "${logger}|${message}";
        fileTarget.Layout = "${machinename}|${longdate}|${logger}|${level:uppercase=true}|${message}|${exception:separator=\r\n:format=message,type,method,stackTrace:maxInnerExceptionLevel=10:innerExceptionSeparator=\r\n:innerFormat=message,type,method,stackTrace}";

        // Step 4. Define rules
        config.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Trace, consoleTarget));
        config.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Trace, fileTarget));

        // Step 5. Activate the configuration
        LogManager.Configuration = config;
        LogManager.EnableLogging();
    }
}

}

我是关于PostSharp的工作原理的新手。我已经使用NLog编写了一些代码,希望使用PostSharp进行改进。 Nlog输出正常(输出在控制台和文件上均完成)。 ...

postsharp
1个回答
0
投票

PostSharp仅记录带有[Log]注释的方法,并且仅在设置了后端后才开始记录日志,就像您在InitPostSharp中所做的那样。

如果您将以下方法添加到班级Program

© www.soinside.com 2019 - 2024. All rights reserved.