This Article describe how to get the event viewer log and clear the data from .Net and Save the event viewer into database in Coldfusion
To do this we need to create a class in .Net which will get the event viewer log and clear the entry and return the data as datatable which can be used from coldfusion and save the same in databasse
Create the table using the query below
CREATE TABLE EVENTVIEWERDATA ( EVENTID INTEGER, ENTRYTYPE VARCHAR2 (200), MESSAGE VARCHAR2 (4000),TIME VARCHAR2 (800))
.Net Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;public class EvtViewerSample
{public DataTable getEventviewer(String EventLogName)
{
// Create the object for EventLog
EventLog ev = new EventLog(EventLogName, System.Environment.MachineName, "MyAppName");
//Create the columns for the datatable
DataTable CSDT = new DataTable();
CSDT.Columns.Add("EventId");
CSDT.Columns.Add("Message");
CSDT.Columns.Add("EntryType");
CSDT.Columns.Add("Time");
try
{
// Loop through the data and add a row
foreach (EventLogEntry entry in ev.Entries)
{
DataRow drdummy = CSDT.NewRow();
drdummy["EventId"] = entry.InstanceId.ToString();
drdummy["Message"] = entry.Message.ToString();
drdummy["EntryType"] = entry.EntryType.ToString();
drdummy["Time"] = entry.TimeGenerated.ToString();
CSDT.Rows.Add(drdummy);
}
ev.Clear(); // Clear the log information
ev.Close(); // Close the connection
return CSDT;
}
catch (Exception ex)
{
throw ex;
}
}
}
CFM Code
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Eventviewer Demo</title>
</head> <body> <!—CAll the .Net method by passing the event log name like Application or Security or SystemNote : Current code will clear the Log file from event Viewer Please dont try this in Production database
—->
<cfobject
type = “dotnet”
name = “EvtDemo”
class = “EvtViewerSample”
assembly =”d:/Vishnu/EvtViewerSample.dll”>
<cftry>
<strong>Note: All Event log entried are cleared and saved in the database </strong>
<cfset Qrydata = #EvtDemo.getEventviewer(“Security”)#>
<cfloop query=”Qrydata”>
<cfquery name=”InsData” datasource=”CWtestsrv”>
Insert into EVENTVIEWERDATA (EVENTID, ENTRYTYPE,MESSAGE,TIME)
Values (
‘#Qrydata.EVENTID#’,
‘#Qrydata.ENTRYTYPE#’,
‘#Qrydata.MESSAGE#’,
‘#Qrydata.TIME#’)
</cfquery>
</cfloop>
<cfdump var=”#Qrydata#”>
<cfcatch type=”Any” >
<h3>Error Occured</b></h3>
<cfoutput>
<!— and the diagnostic message from the ColdFusion server —>
<p>#cfcatch.message#</p>
<p>Caught an exception, type = #CFCATCH.TYPE# </p>
<p>The contents of the tag stack are:</p>
<cfloop index = i from = 1
to = #ArrayLen(CFCATCH.TAGCONTEXT)#>
<cfset sCurrent = #CFCATCH.TAGCONTEXT[i]#>
<br>#i# #sCurrent["ID"]#
(#sCurrent["LINE"]#,#sCurrent["COLUMN"]#)
#sCurrent["TEMPLATE"]#
</cfloop>
</cfoutput>
</cfcatch>
</cftry>
Code Samples avilable in the link
https://share.adobe.com/adc/document.do?docid=91bcf306-7405-11dc-b75f-151d3f6d9313