如何在 log4net 布局模式中插入选项卡?
我的需求很简单 - 我希望能够在 Excel 中打开我的文本日志文件,以便它自动将其分成与日志字段匹配的列。
为此,我需要用制表符分隔日志字段。
我的模式是: %utcdate [%thread] %-5level %logger - %message%newline
我需要类似的东西: %utcdate%tab[%thread]%tab%-5level%tab%logger%tab%message%newline
谢谢。
My need is simple - I want to be able to open my text log file in excel, so that it automatically breaks it in columns matching the log fields.
For that, I need the log fields separated with a tab.
My pattern is: %utcdate [%thread] %-5level %logger - %message%newline
I need something like:%utcdate%tab[%thread]%tab%-5level%tab%logger%tab%message%newline
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
警告:我实际上没有使用过 log4net。但如果我理解正确的话,配置是一个 XML 文件,不是吗?模式只是带有一些特殊标记的文本。那么您是否尝试过在模式中嵌入实际的制表符?选项卡的 XML 序列是
,例如:
或者如果您以其他方式提供模式(可能通过
PatternString
构造函数 或其他),只需在您传递的字符串中包含制表符该构造函数的文档遵循 PatternLayout
文档来讨论字符串本身,并且 他们说:(他们的强调。)无论如何,值得一试......
Caveat: I haven't actually used log4net. But if I understand correctly, the configuration is an XML file, isn't it? And the pattern is just text with some special tokens. So have you tried embedding actual tab characters in your pattern? The XML sequence for a tab is
, e.g.:
Or if you're supplying the pattern some other way (perhaps via the
PatternString
constructor or whatever), just include tab characters in the string you're passing in. The docs for that constructor defer to thePatternLayout
docs to talk about the string itself, and there they say:(Their emphasis.) Worth a try, anyway...
这对我有用:
1)实例化日志记录类时,添加以下行:
2)然后在 log4net XML 中,引用新创建的 log4net 属性。例如:
This worked for me:
1) When your logging class is instantiated, add this line:
2) Then in the log4net XML, make a reference to your newly-created log4net property. For example:
您可以在模式中输入制表符而不转义(请参阅级别和日期之间的空格):
我无法在 Visual Studio 中编写制表符,因为它会写入空格,但我在记事本++中输入制表符复制它(ctrl+c ctrl +v) 并且它正在工作。
编辑:堆栈溢出用空格替换了我的选项卡。所以你需要输入你自己的选项卡
You can type tab into pattern without escaping (see space between level and date):
I cant write tab in visual studio, because it writes spaces, but I typed tab into notepad++ copy it (ctrl+c ctrl+v) and it is working.
EDIT: stack overflow replaced my tab with spaces. So you need to type your own tab
如果这只是关于 Excel,那么您可以使用其他分隔符,甚至可能
;
就足够了。另一种选择是编写您自己的模式转换器。可以找到一个示例 这里。
If this is just about Excel then you could use some other separator, maybe even a
;
would be good enough.Another option would be to write your own pattern converter. An example can be found here.
log4j2 模式布局中的选项卡为:\t
A tab in log4j2 pattern layout is: \t