Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts

May 5, 2008

How to log Events from ASP

How to write Events to eventlog by ASP

 

Open run command à Type notepad

Open notepad à Type/Copy/Paste the code below into the notepad and save as Test.asp.

 

<%@ Language=VBScript %>

<HTML>

<BODY>

<% 'Use these Constants to designate the type of Event Log.

const SUCCESS = 0

const ERROR = 1

const WARNING = 2

const INFORMATION = 4

const AUDIT_SUCCESS = 8

const AUDIT_FAILURE = 16

dim WshShell set WshShell = Server.CreateObject("WScript.Shell")

wshshell.Logevent WARNING, " Event Logged Successfully by Windows Script Host!"

set wshshell=nothing

Response.write "Event Logged Successfully by Windows Script Host!" %>

</BODY>

</HTML>

 

Now copy the test.asp and place into C:\Inetpub\wwwroot à hit the browser as http://localhost/test.asp

OR

create a folder name Test and put this test.asp into this folder. Right click on Test folder à Click Web Sharing Tab à Share This Folder àKeep Name as Test à Apply OK. à Now hit http://localhost/test/test.asp

 

Now open run command à Type eventvwr à See application events à You will get logged your events to EventLog

Enjoy J

Ref: For more information visit http://support.microsoft.com/kb/301309

Apr 24, 2008

How to write Text / Trace / Log file using ASP?

How to write Trace file using ASP?

Previously I wrote for how to write text file with ASP , Now one step forward how to write trace file with ASP?

So here we goes.

Create a text file .

1. Create an File system object that is used for File Access

2. Get the File Path

3. Create the file if not exist.

4. Open the File

5. Log the Server Variables

6. Close the File object release all memory

set FSO = Server.CreateObject("scripting.FileSystemObject")

Dim filePath

filePath = Server.MapPath("Trace.txt");

set myFile = fso.CreateTextFile(filePath ,8, true) 'Here 8 means we using for appending

myFile.WriteLine(Request.servervariables("SERVER_NAME"))

myFile.WriteLine(Request.servervariables("SCRIPT_NAME"))

myFile.WriteLine("Write more looging tuff")

myFile.Close

How to write text file using ASP?

How to write text file using ASP?

Previously I wrote for how to write text file with C#, Now one step back how to write text file with ASP?

So here we goes.

set FSO = Server.CreateObject("scripting.FileSystemObject")

This line creates an object that is used for File Access

set myFile = fso.CreateTextFile("C:\Trace.txt", true)

This creates a blank text file object for us to use.The CreateTextFile object creates a text file, based on what we specified. The first option specifies the location of the text file, while the second one states whether or not to create the file if one doesn't exist. Very simple methods.

Next, we do something simple

myFile.WriteLine("Write whatever you want to put")

myFile.WriteLine("Write some more stuff")

WriteLine writes the text to the text file.

myFile.Close

This closes the file and free the memory.

Now for Reading:

First, we create an object, but not after defining some constants

Const ForReading = 1, ForWriting = 2, ForAppending = 8

This just allows us the different methods of working with text, when we have an open text file.

set fso = server.CreateObject("Scripting.FileSystemObject")

Here we create our object again.

set f = fso.GetFile("C:\Trace.txt")

Here we state that we will be manipulating the file C:\Trace.txt . Moderately simple. Now we need to open it as a stream. Get ready, here comes the hard stuff ;-)

set ts = f.OpenAsTextStream(ForReading, -2)

This creates a text stream with our file. It is like a continous flow of information. Now we jsut read a line

TextStreamTest = ts.ReadLine

This code reads a line of information, and puts it into TextStreamTest. Pretty simple, isnt it?! I hope so. Anyway, I've discussed the code, and I shall let you go.

One last note. Let's say you wanted to read all the contents out a text file. Then you would do this

Do While not ts.AtEndOfStream

myText = myText & ts.ReadLine & vbCrLf

Loop

Hope you learn something !! Write your comments !! Thanks !!