流畅的 log4net 配置

发布于 2024-08-16 19:34:47 字数 141 浏览 6 评论 0原文

任何人都知道是否有一种流畅的方式来配置 log4net(附加程序和附加程序的所有属性等...)。

xml快把我逼疯了。

或者如果没有,是否有人知道一个不错的 .Net 日志框架,可以轻松流畅地配置并提供与 log4net 类似的功能?

Anyone know if there's a fluent way of configuring log4net (appenders and all the properties for appenders etc...).

The xml is driving me crazy.

Or if not, does anyone know of a decent .Net logging framework that can easily be fluently configured and offer similar features to log4net?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诺曦 2024-08-23 19:34:47

酷酷的欢呼我会看看。在 log4net 中也找到了我要找的东西。

令人烦恼的是,我们已经开始使用 Castle Logging Facility,它似乎只将字符串写入 xml 文件!因此,可能必须考虑通过 DSL 完成所有操作并生成 xml 配置作为预构建步骤。

    private static void ConfigureLog()
    {
        var root = ((Hierarchy)LogManager.GetRepository()).Root;
        root.AddAppender(GetConsoleAppender());
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "standard.log", Level.Debug));
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "error.log", Level.Warn));
        root.Repository.Configured = true;
    }

    private static FileAppender GetFileAppender(string directory, string fileName, Level threshold)
    {
        var appender = new FileAppender
        {
            Name = "File", 
            AppendToFile = true,
            File = directory + "\\" + fileName, 
            Layout = new PatternLayout(_pattern), 
            Threshold = threshold
        };

        appender.ActivateOptions();
        return appender;
    }

    private static ConsoleAppender GetConsoleAppender()
    {
        var appender = new ConsoleAppender
        {
            Name = "Console", 
            Layout = new PatternLayout(_pattern), 
            Threshold = Level.Debug
        };

        appender.ActivateOptions();
        return appender;
    }

Cool cheers I'll take a look. Found what I was looking for in log4net too.

Annoyingly we've started using Castle Logging Facility which only seems to take a string to an xml file! So may have to consider doing it all via DSL and generating our xml configs as a pre build step.

    private static void ConfigureLog()
    {
        var root = ((Hierarchy)LogManager.GetRepository()).Root;
        root.AddAppender(GetConsoleAppender());
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "standard.log", Level.Debug));
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "error.log", Level.Warn));
        root.Repository.Configured = true;
    }

    private static FileAppender GetFileAppender(string directory, string fileName, Level threshold)
    {
        var appender = new FileAppender
        {
            Name = "File", 
            AppendToFile = true,
            File = directory + "\\" + fileName, 
            Layout = new PatternLayout(_pattern), 
            Threshold = threshold
        };

        appender.ActivateOptions();
        return appender;
    }

    private static ConsoleAppender GetConsoleAppender()
    {
        var appender = new ConsoleAppender
        {
            Name = "Console", 
            Layout = new PatternLayout(_pattern), 
            Threshold = Level.Debug
        };

        appender.ActivateOptions();
        return appender;
    }
神经暖 2024-08-23 19:34:47

如果您不介意对 Microsoft 企业库的依赖,则可以使用 日志记录应用程序块。配置仍然是XML格式,但是你可以直接在Visual Studio中通过图形界面进行编辑,

If you don't mind the dependency on Microsoft's Enterprise library, you could use the Logging Application Block. Configuration is still in XML, but you can edit through a graphical interface directly in Visual Studio,

掀纱窥君容 2024-08-23 19:34:47

我对基于 XML 的 log4net 配置感到痛苦。不久前,我对调试它感到非常沮丧,因此我开始为 log4net 构建自己的流畅 API。在 github 上查看 Fluent-log4net 。虽然还不是所有内容都得到 100% 支持,但已经实现了。

我会说,它确实使测试和查看配置变得更加容易。

I feel your pain with the XML-based log4net configuration. I got frustrated enough with debugging it a while ago that I started putting together my own fluent API for log4net. Check out fluent-log4net over on github. Not everything is 100% supported yet, but it's getting there.

It does make testing a viewing the configuration at a glance a lot easier, I will say.

以酷 2024-08-23 19:34:47

出于这个原因,我已经转移到 NLog

I have since moved to NLog for this very reason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文