Delphi中写入事件日志

9

我该如何让应用程序将调试文本写入Delphi IDE(Borland Developer Studio 2006)的事件日志窗口?

如何更改文本颜色?


使用 dbgview 可以拥有颜色、过滤器等功能。 - Harriv
4个回答

26

OutputDebugString('Hello,World');

我认为您可能需要在“uses”列表中添加Windows。但我不确定…

据我所知,文本颜色无法更改:这是Delphi IDE的一个特性,它会将线程启动/停止、DLL加载/卸载等额外消息添加到该窗口中,并具有自己特定的颜色。


8

是的,你可以使用OutputDebugString

如果你想要更强大的控制和管理调试输出的功能,比如高亮过滤器,你应该使用DebugView

注意:当你在Delphi IDE中运行应用程序时,DebugView无法捕获调试日志。


6
procedure Write2EventLog(Source,Msg: string);
var h: THandle;
    ss: array [0..0] of pchar;
begin
    ss[0] := pchar(Msg);
    h := RegisterEventSource(nil,  // uses local computer
             pchar(Source));          // source name
    if h <> 0 then
      ReportEvent(h,           // event log handle
            EVENTLOG_ERROR_TYPE,  // event type
            0,                    // category zero
            0,        // event identifier
            nil,                 // no user security identifier
            1,                    // one substitution string
            0,                    // no data
            @ss,     // pointer to string array
            nil);                // pointer to data
    DeregisterEventSource(h);
end;

3
Delphi集成开发环境中的“事件日志”窗口与Windows事件日志无关。可能令人感到困惑,我知道! - Roddy
5
虽然有些混淆,但仍然正确且有用,因为在谷歌搜索“将Delphi错误写入Windows事件日志”时,它是第一个搜索结果。 - LMSingh

3
除了已经提到的(即OutputDebugString和使用DebugView替代内置日志查看器),您可以通过选项更改日志视图中消息的颜色。最简单的方法是在日志窗格中右键单击,然后从上下文菜单中选择“属性”。在出现的选项卡上,您可以在“颜色”部分设置“输出调试字符串”要使用的颜色。显然,这将更改通过OutputDebugString发出的所有消息的颜色-它不会允许个别着色。为此,最好使用DebugView的过滤器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接