Writing to log file using code-behind form submit
bbcompent1 | Posted 2:17pm 12. December 2008 Server Time |
Ok, this is kind of a tricky one. I'm trying to figure out how in C# I can use the onclick event of the submit button to write to a text file. I can write to it just fine for logging on, timeout and logoff. What I'd like to do is trace the user's activity, when they click a link, it writes the click to a file. Here is my code:
if (GroupVals.Contains(fileman)) //NT Group check
{
string MyUrl= "https://www.fileman.com/LoginSubmit.do";
Label2.Text = "<tr><td>
<form name='form2' id='form2' method='POST' action='" + MyUrl + "' style='margin: 0px; padding: 0px;'>
<input type='hidden' name='method' value='loginSubmit' style='width:0px'>
<input type='hidden' name='js' value='n' style='width:0px'>
<input type='hidden' name='username' value='" + uname + "' style='width:0px'>
<input type='hidden' name='password' value='" + pwd + "' style='width:0px'>
<input type='hidden' name='domain' value='EHGT' style='width:0px'>
<button type='submit' class='textButton' style='width:145px' onclick=''>
<img src='images/navimg/efx_img.png' border='0'></button>
</form></td></tr>";
}
The code I have that writes to the log file is:
using (StreamWriter tw = File.AppendText(Server.MapPath("log\\logonlog.txt")))
{
tw.Write("Logoff,");
tw.Write(Session["TrueUserId"] + ",");
tw.Write(DateTime.Now + ",");
tw.WriteLine("");
tw.Close();
}
|
bbcompent1 | Posted 2:17pm 12. December 2008 Server Time |
What I'd like to do is get the log writer code to work in an onclick event. FYI, this is all code-behind in C#.
bbcompent1 | Posted 9:11am 19. December 2008 Server Time |
Hmmm, no one even has an idea? I'll check back later.
bbcompent1 | Posted 12:24am 17. March 2009 Server Time |
Maybe I could utilize the newer variable OnClientClick since OnClick is not an option?
stimpy | Posted 6:06pm 31. March 2009 Server Time |
Try this: (part 1)
Okay so you modify the login control to be a template then add a click event to the button OnCLick=LoginButton_Click"
Switch to code behind and add the following functions and using statements:
using System.IO;
using System.Text;
protected void LoginButton_Click(object sender, EventArgs E)
{
string LogFile = generateLogFileName()
writeToLogFile(LogFile);
}
private static string generateLogFileName()
{
string fileName; // put your file name create stuff here like Year + Month + Day + Time(HH:MM:SS) + ".log"
return fileName
}
/// <summary>
/// Create an empty text file that we can append text to as we go.
/// </summary>
/// <param name="outputFile">The name of the outpu file to create.</param>
private static void createTextFile(string outputFile)
{
// Create an instance of the FileInfo Object which
// will be used to create our output text file
FileInfo fInfo = new FileInfo(@outputFile);
// Create a StreamWriter instance with our output file name
// and close it as we'll just append each line as we read it.
StreamWriter sWriter = fInfo.CreateText();
sWriter.Close();
}
stimpy | Posted 6:07pm 31. March 2009 Server Time |
Part 3:
/// <summary>
/// Receives a string as input and pads a specific number of characters onto the back of the string.
/// It pads the 'source' string by adding the string 'padString' ('size' - src.Length) times to 'src'.
/// If 'padString' only consists of one character e.g. a 0 or zero,, this corresponds to pad 'src'
/// to length 'size'.
/// </summary>
/// <param name="source">The source number to be padded.</param>
/// <param name="size">The size the number is to be padded to <example>8 characters in length</example>.</param>
/// <param name="padString">The character to pad the source string with, <example>0</example>.</param>
/// <returns>A string of the desired length with the appropriate number of pad characters added to the
/// back of the string.
/// </returns>
public static string padStringBack(string source, int size, string padString)
{
for (int n = size - source.Length; n > 0; n--)
source += padString;
return source;
}
Now let's write to a text file. We are presuming you have the values you want to write to the text file in
stimpy | Posted 6:07pm 31. March 2009 Server Time |
Part 2:
/// <summary>
/// Appends the incoming text to the appropriate output text file.
/// </summary>
/// <param name="fileToAppendName">The output file to append the text to. Should be in the form of
/// DeptName + "_" + Mnemonic + ".dat" <example>PCARD_Some_Mnemonic.dat</example>.</param>
/// <param name="textToAppend">The text to append to the output file</param>
private static void appendToTextFile(string fileToAppendName, string textToAppend)
{
// Create a StreamWriter object that we'll use
// to append the text to the file
using (StreamWriter sWriter = File.AppendText(@fileToAppendName))
{
// This text is added to allow our file to grow over time
// With Each append
sWriter.WriteLine(textToAppend);
// Not sure if thisis needed as the "using construct"
// should handle all the garbage collection automatically
// which should include closing the stream, but just in case
sWriter.Close();
}
}
/// <summary>
/// Receives a string as input and pads a specific number of characters onto the front of the string.
/// It pads the 'source' string by adding the string 'padString' ('size' - src.Length) times to 'src'.
/// If 'padString' only consists of one character e.g. a 0 or zero,, this corresponds to pad 'src'
/// to length 'size'.
/// </summary>
/// <param name="source">The source number to be padded.</param>
/// <param name="size">The size the number is to be padded to <example>8 characters in length</example>.</param>
/// <param name="padString">The character to pad the source string with, <example>0</example>.</param>
/// <returns>A string of the desired length with the appropriate number of pad characters added to the
/// front of the string.
/// </returns>
public static string padStringFront(string source, int size, string padString)
{
for (int n = size - source.Length; n > 0; n--)
source = padString + source;
return source;
}
stimpy | Posted 6:08pm 31. March 2009 Server Time |
Part 3:
/// <summary>
/// Receives a string as input and pads a specific number of characters onto the back of the string.
/// It pads the 'source' string by adding the string 'padString' ('size' - src.Length) times to 'src'.
/// If 'padString' only consists of one character e.g. a 0 or zero,, this corresponds to pad 'src'
/// to length 'size'.
/// </summary>
/// <param name="source">The source number to be padded.</param>
/// <param name="size">The size the number is to be padded to <example>8 characters in length</example>.</param>
/// <param name="padString">The character to pad the source string with, <example>0</example>.</param>
/// <returns>A string of the desired length with the appropriate number of pad characters added to the
/// back of the string.
/// </returns>
public static string padStringBack(string source, int size, string padString)
{
for (int n = size - source.Length; n > 0; n--)
source += padString;
return source;
}
Now let's write to a text file. We are presuming you have the values you want to write to the text file in
stimpy | Posted 6:08pm 31. March 2009 Server Time |
Part 4:
private void writeToLogFile(string logFileName)
{
// Check to see if log file already exists
if(!File.Exists(logFileName))
{
// The file doesn't exist so create and recallthe function
createTextFile(loginFileName);
writeToLogFile(loginFileName);
}
else
{
// we have a file so let's write our values to it
try
{
// NOTE you want to do all your variable and value checking prior to this
string textToAppend = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}",padStringBack(Name, 5, " "),
padStringBack(Address, 5, " "), padStringBack(City, 5, " ")...
appemdToTextFile(outputFileName, textToAppend);
}
catch (Exception e)
{
// Handle exception here
}
finally
{
// close the file using the close method example: outputFileName.Close()
}
}
}
and that's pretty much it in a nutshell
Reply to Post Writing to log file using code-behind form submit
|
|
|