Writing to the Event Log in Delphi

后端 未结 4 2189
攒了一身酷
攒了一身酷 2020-12-08 17:49

How can I get an application to write debug text to the Event Log window in the Delphi IDE (Borland Developer Studio 2006)?

How does one change the color of the text

相关标签:
4条回答
  • 2020-12-08 18:00

    OutputDebugString('Hello,World');

    I think you may need to add Windows to your 'uses' list. Not 100% sure on that...

    The text colour can't be changed as far as I know: It's a feature of the Delphi IDE that it adds additional messages into that window for thread start/stop, DLL load/unload, with their own specific colour.

    0 讨论(0)
  • 2020-12-08 18:03

    Yes, you can use OutputDebugString.

    If you want get more powerful features for controlling and managing debug output, such as a highlighting filter, you should use DebugView.

    Note: DebugView can't capture the debug log when you run your application in the Delphi IDE.

    0 讨论(0)
  • 2020-12-08 18:08

    Apart from what has been said (i.e. OutputDebugString and using DebugView instead of the built-in log viewer), you can change the color of messages in the log view via the Options. The easiest way to get there is by right-clicking in the log pane and selecting "Properties" from the context menu. On the tab that will appear you can set the color to use for "Output Debug Strings" from the "Colors" section. Obviously this will change the color of all messages emitted via OutputDebugString - it will not allow individual coloring. For that you'd better use DebugView's filters.

    0 讨论(0)
  • 2020-12-08 18:25
    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;
    
    0 讨论(0)
提交回复
热议问题