[Migrated content. Thread originally posted on 05 January 2011]
This code will show how to using the Trace class from COBOL. Code was converted from old COBOL.Net syntax to the new repository syntax in .Net.
Some Images:-
This has been marked as a Quote in the Post Editor
This is Bold in the Editor.
The code below has been marked with the code attribute
$set sourceformat"variable"
$set ilusing"System.Diagnostics"
program-id. COBOLEventLogger as "COBOLEventLogger".
*****************************************************************
*
* Author - David.Sands@MicroFocus.com (EMEA Support)
*
* Date - Feb 2005 (Amended Jan 2010 for Visual COBOL Syntax)
*
* Purpose - Show how you can use the .Net Trace class
* and EventLogs to add instumentation to your application.
*
* A listener needs to be setup so that the Trace
* is output to the desired location. The Default
* listener will output to the Output Window in
* the Visual Studio debugger.
*
* Other listeners can be added via code or an
* application config file.
*
* Example config file for this is:-
*
* <configuration>
* <system.diagnostics>
* <switches>
* <!-- COBOL Trace can be set from 0 to 4
* 0 is TraceLevel.Off
* 1 is TraceLevel.Error
* 2 is TraceLevel.Warning
* 3 is TraceLevel.Info
* 4 is TraceLevel.Verbose -->
* <add name="COBOLTrace" value="4" />
* </switches>
* <trace autoflush="true" indentsize="4">
* <listeners>
* <add name="COBOLListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\\COBOLDOTNET.log" />
* </listeners>
* </trace>
* </system.diagnostics>
* </configuration>
*
*****************************************************************
****** NO NEED FOR Respository in Visual COBOL
*environment division.
*configuration section.
*repository.
* copy "MFNETSystem.cpy".
* class cTrace as "System.Diagnostics.Trace"
* class cTraceSwitch as "System.Diagnostics.TraceSwitch"
* class cDateTime as "System.DateTime"
* class cEventLog as "System.Diagnostics.EventLog"
* .
data division.
working-storage section.
*01 TraceSwitch object reference CTraceSwitch.
*01 ws-now object reference CDATETIME.
*01 ws-string object reference cString.
*01 ws-log object reference cEventLog.
01 TraceSwitch type System.Diagnostics.TraceSwitch.
01 ws-now type System.DateTime.
01 ws-string string.
01 ws-log type System.Diagnostics.EventLog.
procedure division.
* set TraceSwitch to cTraceSwitch::"New"("COBOLTrace","COBOL Application Trace Switch")
set TraceSwitch to type TraceSwitch::New("COBOLTrace","COBOL Application Trace Switch")
perform a-init.
perform b-main.
perform c-term.
goback.
*************************************************************
* Initialization
*************************************************************
a-init section.
*> The next trace will always come out
invoke type Trace::WriteLine("This is the Initialization")
.
*************************************************************
* MainLine
*************************************************************
b-main section.
invoke type Trace::Indent()
*> The next trace will always come out
invoke type Trace::WriteLine("This is the Main Processing")
*> The next trace will come out only if COBOLSwitch is TraceError or Above
*> ie = 1
invoke type Trace::WriteLineIf(TraceSwitch::"TraceError","TraceError or Above is set")
*> The next trace will come out only if COBOLSwitch is TraceWarning or Above
*> ie >= 2
invoke type Trace::WriteLineIf(TraceSwitch::"TraceWarning","TraceWarning or Above is set")
*> The next trace will come out only if COBOLSwitch is TraceInfo or Above
*> ie >= 3
invoke type Trace::WriteLineIf(TraceSwitch::"TraceInfo","TraceInfo or Above is set")
*> The next trace will come out only if COBOLSwitch is TraceVerbose or Above
*> ie >= 4
invoke type Trace::"WriteLineIf"(TraceSwitch::"TraceVerbose","TraceVerbose or Above is set")
invoke type Trace::Unindent()
Display "Now try to log an Event in the Window Event Logs"
if not type EventLog::SourceExists("COBOLEventDemo")
invoke type EventLog::CreateEventSource("COBOLEventDemo","Application")
end-if
invoke type EventLog::New() returning ws-log
set ws-log::Source to "COBOLEventDemo"
invoke ws-log::WriteEntry("This is an entry from COBOL.Net")
.
*************************************************************
* Termination
*************************************************************
c-term section.
*> The next trace will always come out
invoke type Trace::WriteLine("This is the Termination")
.
end program COBOLEventLogger.
Back to Normal here.
Project has been attached to the post.
Find out more about Visual COBOL.