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
lymanknap | Posted 3:44pm 4. August 2010 Server Time |
211P67 <a href="http://heyvpvdembkq.com/">heyvpvdembkq</a>, [url=http://buookagejhdl.com/]buookagejhdl[/url], [link=http://tcbxefzmdiec.com/]tcbxefzmdiec[/link], http://nncaacjakjeg.com/
lymanknap | Posted 1:00pm 5. August 2010 Server Time |
http://www.viagra-advice.com viagra 0481 http://www.orderlevitrapills.com/ cheapest online cost for levitra >:-PP http://www.tramadol-pills-online.com tramadol %]] http://www.meridiapills.com/ buy meridia without prescription >:) http://www.prozacsearch.com/ prozac nation movie buy 8-]]]
lymanknap | Posted 11:21pm 5. August 2010 Server Time |
W2EvWr <a href="http://gxqlawirmmke.com/">gxqlawirmmke</a>, [url=http://zezrihzmehpd.com/]zezrihzmehpd[/url], [link=http://cvfyfntdlwzk.com/]cvfyfntdlwzk[/link], http://urljwgeaxzrf.com/
lymanknap | Posted 0:03am 6. August 2010 Server Time |
http://www.robkellysurf.com/home_insurance_quotes.html home insurance quotes wvyt http://www.arcataenglishcockers.com/life-insurance-rates.html life insurance quotes 559619 http://www.arcataenglishcockers.com/ health insurance quotes :-[[[ http://www.roomofzen.com/businessinsurancerates.html business insurance quotes 0690
lymanknap | Posted 10:02am 6. August 2010 Server Time |
HH1unM <a href="http://hrdpchdhuqkw.com/">hrdpchdhuqkw</a>, [url=http://dhpjmanzqktw.com/]dhpjmanzqktw[/url], [link=http://ugndutdkjbkj.com/]ugndutdkjbkj[/link], http://iwzftmxgczbu.com/
lymanknap | Posted 2:25am 8. August 2010 Server Time |
http://www.jomamasnybagels.com/ cialis wbczj http://www.drevorezba.net/ xanax yaitfi http://www.hbtnstyle.com/ accutane %]] http://www.tcfhoustonnorthwest.org/ diet pill acomplia qaf
lymanknap | Posted 1:15am 9. August 2010 Server Time |
http://www.horseswithhope.com/pricelist_ultram.htm ultram >:))) http://www.maranguapefutebolclube.com/pricelist_cialis.html cialis online 637 http://www.anascottage.com accutane online %[[[ http://www.renatocardoso.net/ultram.html ultram online 8OOO
lymanknap | Posted 12:00am 9. August 2010 Server Time |
http://www.mega1031.org/ home insurance gpmj http://www.slotsguidance.com/ slots ihycw http://www.getlifeinsurancequotes.net/ life insurance yot http://www.cheapinsurancemate.com/ cheapest insurance 095
choonchan | Posted 7:06am 10. August 2010 Server Time |
vppyvA <a href="http://xvprjhbgaoyn.com/">xvprjhbgaoyn</a>, [url=http://mwbqjffbraye.com/]mwbqjffbraye[/url], [link=http://rgwbphjckeja.com/]rgwbphjckeja[/link], http://hdyhsscaulrs.com/
choonchan | Posted 8:25am 11. August 2010 Server Time |
http://www.gihberkeley.org/zanaflex.html zanaflex without a prescription 3955 http://www.gihberkeley.org/cipro.html cipro generic 8-))) http://www.gihberkeley.org/norvasc.html norvasc without prescription :( http://www.foodieindenial.com/flomax.html flomax :]]
choonchan | Posted 11:48am 11. August 2010 Server Time |
http://www.carsinsurance4u.com/ infinity auto insurance %))) http://www.getlifeinsurancequotes.net/ life insurance 180 http://www.free-home-insurance-quotes.net/ louisiana homeowners insurance 957 http://www.cheapinsurancemate.com/ cheap insurance grfpsp http://www.getautoinsurancerates.net/ auto insurance agency =PPP
choonchan | Posted 3:04pm 11. August 2010 Server Time |
http://www.bee-gees.info/healthinsurance.html health insurance %-]]] http://www.hotkumquat.com/life-insurance-rates.html life insurance quotes fqky http://www.ihustlelive.com/autoinsurancequotes car insurance rates diijo http://www.karenmartinezforassembly.org/health_insurance.html personal health insurance jbqyq http://www.bee-gees.info/businessinsurance.html business insurance czqniy
choonchan | Posted 6:37pm 11. August 2010 Server Time |
http://www.ganjahcultivo.com/metoprolol.html Lopressor online cviy http://www.ganjahcultivo.com/cialis.html compare cialis levitra viagra 675 http://www.ganjahcultivo.com/ultram.html ultram xgdjik http://www.mediaspecblog.com/ online pharmacy accutane 43610 http://www.montanawaterproperty.com/ cialis %OOO
choonchan | Posted 4:04pm 12. August 2010 Server Time |
http://www.maranguapefutebolclube.com/tramadol.html tramadol online 4134 http://www.themotherscurse.com/cialis.html cialis online =] http://www.gunlawforum.com/ accutane online uvid http://www.moistbeavermagazine.com/prednisone.html prednisone online okq http://www.kalipaksi.com/ bill consolidation buy tramadol 24660
choonchan | Posted 2:55am 14. August 2010 Server Time |
http://www.fantsrestaurant.com/ accutane =PP http://www.postaisdefatima.com/ valium vja http://www.hbtnstyle.com/ accutane online pharmacy zambv http://www.zanortepride.com/ propecia bdleio http://www.aqueerexistence.com/ prednisone 425275
choonchan | Posted 10:59pm 14. August 2010 Server Time |
http://www.mega1031.org/ florida home insurance nrz http://www.get-auto-insurance-quotes.net/ car insurance in florida 9721 http://www.findcarinsurancequotes.net/ auto insurance rates baa http://www.free-home-insurance-quotes.net/ home owner's insurance 7928 http://www.homeinsurance4u.net/ home insurance :-))
choonchan | Posted 1:11pm 16. August 2010 Server Time |
http://www.eltoromn.com/health_insurance.htm health insurance sdjwqg http://www.theworryfreelife.com/health-insurance-quotes.html health insurance quotes bcqdpa http://www.eltoromn.com/home_insurance.htm home insurance :-OOO
choonchan | Posted 10:31pm 17. August 2010 Server Time |
http://www.buyphenterminenow.net/ order phentermine tsdw http://www.bestsleepingpill.net/ ambien 99353 http://www.forget-acne.com/ accutane 808850 http://www.phentermineguide.net/ phentermine =O
mkaymer | Posted 9:44pm 19. August 2010 Server Time |
http://www.carsinsurance4u.com/ car insurance jmiabf http://www.buycheaphomeinsurance.com/ cheap home insurance :OOO http://www.findcarinsurancequotes.net/ car insurance quotes 74655
mkaymer | Posted 3:24am 20. August 2010 Server Time |
http://www.maddonnasnashville.com/cialis.php cialis 8)))
mkaymer | Posted 6:02am 21. August 2010 Server Time |
http://www.gihberkeley.org/strattera.html strattera :P http://www.homeofcarolinacirclemall.com/detrol.htm detrol vcq http://www.foodieindenial.com/depakote.html depakote 339946
mkaymer | Posted 10:09am 21. August 2010 Server Time |
http://viagracomparisons.com viagra alternatives =-O http://weightlosspillsonline.org acomplia medication qmvwyv http://weightlosspillsonline.org/ buy cheap acomplia online :[
mkaymer | Posted 7:07pm 24. August 2010 Server Time |
http://www.puremeds.net/ Prednisone wkdsj http://www.medleader.net/ cialis ojxejs http://www.tramad0l.com/ tramadol >:[[ http://www.mensmeds.net/ cialis 202471
mkaymer | Posted 11:58am 27. August 2010 Server Time |
http://www.petetownshendisinnocent.com/accutane-prices.html cheap accutane online %]
mkaymer | Posted 4:13am 29. August 2010 Server Time |
http://www.arcataenglishcockers.com/auto-insurance-rates.html online auto insurance quotes 6762 http://www.bee-gees.info/healthinsurance.html health insurance acaky http://www.ambermadison.tv/ buy auto insurance online 8-[[ http://www.pinkcollarclub.net/lifeinsurance.html pennsylvania life insurance 7665 http://www.bee-gees.info/lifeinsurance.html life insurance 6141
mkaymer | Posted 6:07am 29. August 2010 Server Time |
http://www.twinsmobiledetailing.com/ cheap auto insurance pgzz http://www.fantagebetas.com/home-insurance.html home inspector insurance =-)) http://www.fantagebetas.com/life-insurance.html life insurance >:-[[[ http://www.steppingintothelight.net/ auto insurance quotes 670 http://www.fantagebetas.com/health-insurance.html health care insurance 8-(
mkaymer | Posted 9:37am 29. August 2010 Server Time |
http://www.pragmablog.com/ acomplia 8-( http://www.ussui.com/ buy valium from europe online dne http://www.tennismaschile.com/ambien.html ambien rx %-))) http://www.kalio.info/ ambien news >:[
mkaymer | Posted 0:34am 31. August 2010 Server Time |
http://www.ranbirkapoorfan.com/ description of valium diazepam tablets >:-( http://www.serotoninsecrets.com/ accutane qmbr http://www.tbt721.com/ prednisone kglhr http://www.urbanprophet.net/ phentermine hoodia 7114
mkaymer | Posted 8:21am 31. August 2010 Server Time |
http://www.ranbirkapoorfan.com/ valium 5727 http://www.urbanprophet.net/ phentermine 8-) http://www.ihpvirginia.org/ xanax valium 578
mkaymer | Posted 12:34am 31. August 2010 Server Time |
http://www.eltoromn.com/health_insurance.htm health insurance qidhgj http://www.theworryfreelife.com/auto-insurance-quotes.html car insureance 8PP http://www.theworryfreelife.com/health-insurance-quotes.html health insurance quotes %-OOO
mkaymer | Posted 2:35pm 31. August 2010 Server Time |
http://www.mariasharapovapictures.com/ acomplia 575 http://www.thenewlicious.com/ ambien glf http://www.newoxfordmovement.org/ prednisone :[[[ http://www.tcfhoustonnorthwest.org/ acomplia lose weight stop smoking 960
mkaymer | Posted 4:34pm 31. August 2010 Server Time |
http://www.mega1031.org/ homeowner s insurance 2445 http://www.carsinsurance4u.com/ car insurance 947 http://www.autosinsurancequotes4u.com/ auto insurance quotes ymagw
Reply to Post Writing to log file using code-behind form submit
|
|
|