.NET 应用程序中没有 .config 文件的 LogEntries

LogEntries without a .config file in a .NET application

我正在阅读有关从找到的 .NET 应用程序进行日志记录的文章 here。 我想在不使用 .config 文件的情况下执行等效的功能。有人有代码示例吗? 谢谢

我们当然可以做到。对于 nlog,我们可以执行以下操作。

        class Program
        {
            public static LogentriesTarget target = new LogentriesTarget();
            private static Logger logger = LogManager.GetCurrentClassLogger();
            static void Main(string[] args)
            {
                var config = new LoggingConfiguration();
                target.Token = "LOG_TOKEN";
                target.Ssl = false;
                target.Debug = true;
                target.Name = "Logentries";
                target.Layout = "${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}";
                target.HttpPut = false;
                config.AddTarget("Logentries2", target);
                var loggingRule = new LoggingRule("*", LogLevel.Debug, target);
                LogManager.Configuration.AddTarget("targetName", target);
                LogManager.Configuration.LoggingRules.Add(loggingRule);
                LogManager.Configuration.Reload();
                logger = LogManager.GetCurrentClassLogger();
            }
        }

对于 log4net,我们可以执行以下操作。

        class Program
        {
            private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
            static void Main(string[] args)
            {
                LogentriesAppender appender = new LogentriesAppender();
                appender.Token = "YOUR_TOKEN";
                appender.Name = "LogentriesAppender";
                PatternLayout layout = new PatternLayout();
                layout.ConversionPattern = "%d{ddd MMM dd HH:mm:ss zzz yyyy} %logger %: %level%, %m,";
                layout.ActivateOptions();
                appender.Layout = layout;
                Logger l = (Logger)logger.Logger;
                l.AddAppender(appender);
                logger.Debug("Hi");
            }
        }

最后,另一种选择是使用 Serilog 将您的日志数据发送给我们。

此致, 斯蒂芬

我认为这段代码有误...你能检查一下吗?顺便说一句,我是这样工作的:

    target.Token = "LOG_TOKEN";
    target.Ssl = false;
    target.Debug = true;
    target.Name = "Logentries";
    target.Layout = "${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}";
    target.HttpPut = false;

    var config = new LoggingConfiguration();
    config.AddTarget("Logentries", target);

    var loggingRule = new LoggingRule("*", LogLevel.Debug, target);
    config.LoggingRules.Add(loggingRule);

    LogManager.Configuration = config;
    LogManager.Configuration.Reload();