【问题标题】:How to Log an OutputDebugString via Log4Perl如何通过 Log4Perl 记录 OutputDebugString
【发布时间】:2013-07-23 15:05:03
【问题描述】:

是否可以使用 Log4Perl 将所有消息/级别直接记录到 OutputDebugString(Windows 系统)中?

我有一些已经使用 log4perl 的模块,但现在我想在所有日志消息都是 OutputDebugStrings 的环境中使用这些模块 - 在这种环境中,消息是用 DbgView 读取的,我也想读取我的模块的输出使用 DbgView。我不想将 Log4Perl 日志文件与 DbgView 输出合并。

我使用以下 perl 代码直接从 Perl 编写 OutputDebugStrings:

use Win32::API::OutputDebugString qw(OutputDebugString DStr);
OutputDebugString("Foo bar", "baz\n"); # sends  Foo barbaz\n to the debugger

我找到了 log4net.Appender.OutputDebugStringAppender - 我怎样才能为 Perl 实现同样的功能

提前致谢。

【问题讨论】:

  • 您好,我知道如何编写OutputDebugStrings,但我希望所有log4perl 日志消息都通过OutputDebugStrings,Win32::API 如何帮助我?
  • 嗨,我更新了我的问题。使用 Win32::API::OutputDebugString qw(OutputDebugString DStr); OutputDebugString("Foo bar", "baz\n"); # 发送 Foo barbaz\n 到调试器

标签: perl log4perl outputdebugstring


【解决方案1】:

====找到解决方案==== 感谢 daxim,我编写了自己的附加程序。这是来源

-使用以下代码生成一个perl包

package Log4PerlOutputDebugStringAppender;

sub new {
   my($class, %options) = @_;
   my $self = { %options };
   bless $self, $class;
   return $self;
}

sub log {
   my($self, %params) = @_;
   use Win32::API::OutputDebugString qw(OutputDebugString DStr);
   OutputDebugString( "$params{message}" );
}

1;

-使用以下内容生成 Log4Perl 配置文件

log4perl.logger = INFO, OutputDebugString
log4perl.appender.OutputDebugString=Log4PerlOutputDebugStringAppender
log4perl.appender.OutputDebugString.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.OutputDebugString.layout.ConversionPattern=[%-5p] %-15.15c{1} (%4L) - %m%n

-在你的 Perl 脚本中初始化记录器

use Log::Log4perl qw(:easy);
Log::Log4perl->init("logconf.txt");
INFO("INFO");
WARN("WARN");
FATAL("FATAL");

等等 - 所有日志消息都通过 OutputDebugStrings

感谢 daxim 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多