How to delete duplicate lines written by page reload? - c#

I write a log file and it always has a duplicate line (cause by page reloading I guess).
userName searched for: 'assembly' at: 3/24/2015 7:32:42 AM
userName searched for: 'assembly' at: 3/24/2015 7:32:43 AM
Here is my code for file writing:
using (StreamWriter streamWriter = File.AppendText(Server.MapPath("~/searchlog.txt")))
{
streamWriter.WriteLine(userRecord.name + " searched for: \'" + ProcessInputClause + "\' at: " + DateTime.UtcNow);
}
What would be a good way to get rid of this? I am thinking of using:
var lines = System.IO.File.ReadAllLines("...");
System.IO.File.WriteAllLines("...", lines.Take(lines.Length - 1).ToArray());
But this doesn't seem to work...

You shouldn't remove any record from your log. You need precisely know what happens in application to be able reproduce application bugs. Removing line from log can hide something very important.
In case there is need remove some duplication from log than there is probably some design smell.

You can try this:
var previoustext = new HashSet<string>();
File.WriteAllLines(destinationFile, File.ReadLines(sourceFile)
.Where(line => previoustext.Add(line)));

You should change the place you write your log since some modules of the page may call your script multiple times while loading one page.
I wouldn't log the search entries what I would do is insert into database the record that have been searched at what time and other info you need so it is even easier to make reports or statistics of searches done on your website.

Related

Writing a CSV log file

I have a Winforms program that needs to log data points into a .CSV file. It's fairly simple, date/time and a double (the data), and go to the next line.
Here's what I have so far (not working, I get an error saying the file is busy/already open - however, it's empty)
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogFileName = SavePath.Text + "\\LOG\\Seeing-Log-" + TimeNow.ToString("yyyy-MM-dd") + ".csv";
if (!File.Exists(LogFileName))
File.Create(LogFileName);
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
It's that last line that generates the error.
Any idea what I am doing wrong?
thanks
Steve
File.Create returns an open FileStream to the file that's just been created. Either change your code to work with FileStream in both the non-existent and existent file cases, or just close the file after creating it:
if (!File.Exists(LogFileName))
File.Create(LogFileName).Close();
But, of course, if you check the documentation for AppendAllText:
Appends the specified stringto the file, creating the file if it does not already exist.
You'll realise that the above two lines are completely redundant anyway and can be removed:
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
You can even use the free looging tools. Here is one 'log4net'
You can also write the csv file using this. I am assuming currently you are not using logging tool. it will work for you without any code for implementation .
http://element533.blogspot.in/2010/05/writing-to-csv-using-log4net.html
Have a great day!!
Replace
File.Create(LogFileName);
with
File.Create(LogFileName).Close();
see this to create empty file.
The file is locked when you create it. Just update your code to this:
File.Create(LogFileName).Close();

Unable to open PDF programmatically

I'm struggling to open a PDF file inside of Unity. Currently, my application will open up the folder location instead of opening the actual PDF itself.
I've tried using both System.Diagnostics.Process.Startand Application.OpenURL but they all act the same.
Right now, my code looks like:
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Now when I hard code in the file location like below, it opens up the PDF correctly:
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Normally I'd leave it hard coded, but I need to allow one button to open any PDF. How can I resolve this?
The string output of the two lines below are most likely not equal.
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Ensure the paths are the same and you should get the result you expect.
See the documentation for Application.OpenURL here:
http://docs.unity3d.com/ScriptReference/Application-dataPath.html
If you read to the bottom, you'll notice:
"Note that the string returned on a PC will use a forward slash as a folder separator."
This likely the reason why you're getting different results.
Also note that the value of Application.OpenURL changes based on the platform.
string pdfURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "Documents/keyboard-shortcuts-Visual-Studio-Code.pdf";
it return below url and run in browser
http://localhost:1727/Documents/keyboard-shortcuts-Visual-Studio-Code.pdf
thanks
upendra

.Net Application logging

Hello all I have a scenario where i have one winform app as server and infinite number of winform apps as clients
basically each client connects to server and sends a string to server , server than do some calculations and return string back to client, but server have to connect to another server for calculation of that string and in response from that second server our main server stores the response in a string variable and after some specific time intervals it shows that string variable in a textbox but this string gets bigger and bigger after each calculation and hence my server some times starts consuming 1gb memory in task manager and 40% of my cpu usage , and when i removed the string variable my server was running on 45mb of memory and 0-4% of cpu usage i am using string variable like this
string Serverlog += datafetched + "cl"
i have also tried a string builder object but result is same so can any one help me to sort out things ( how can i save logs without consuming to much memory ) and one thing more logs will not be maitained in any file they are only for showing them into textbox
Best solution is to store your logging somewhere, database / file / winlogging / other
What kind of app are you running on the clients? Be aware that u use the AppendText function of the textbox. So dont use:
Textbox.Text += "additional info"
but use
Textbox.AppendText(teTonenTekst + Environment.NewLine);
While logging to a file is best, you mentioned you do not want that.
For UI based logging, I usually avoid a TextBox, and instead use a ListView or DataGridView with hidden gridlines. That way it is easy to truncate the amount of values to a limit, keeping only recent data in the control.
It is also easier to color code different types of logging data.
You can write the text in to the file or MSMQ or Telnet and clear the variable. While displaying the contents read from one of the above mentioned source.
You should be used text file for logging application actions.
I would suggest to store the logs in a file instead of keeping everythingin a variable. I personaly always do that by using a logging function which creates an individual log file for each user each day. Like that you have a better control over your logs and dont have to worry about your preformance problem. Have a look at this example:
internal static void WriteLog(string str, string clientNumber)
{
StreamWriter logWriter = null;
string todayDateString = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
string fullLogFileName = todayDateString + "_" + clientNumber + "_log.txt";
string LogPath = #"\\server\folder\Logs\";
string fullLogFilePathWithName = LogPath + fullLogFileName;
if (!File.Exists(fullLogFilePathWithName))
{
logWriter = new StreamWriter(fullLogFilePathWithName, true);
logWriter.WriteLine(DateTime.Now.ToString("d.MM.yyyy h:mm") + " - " + str);
logWriter.Flush();
}
else
{
logWriter = File.AppendText(fullLogFilePathWithName);
logWriter.WriteLine(DateTime.Now.ToString("d.MM.yyyy h:mm") + " - " + str);
logWriter.Flush();
}
logWriter.Dispose();
logWriter.Close();
}
.net already has built in support for tracing / logging:
http://msdn.microsoft.com/en-us/library/zs6s4h68.aspx
However, we use log4net, and I'm quite happy with it:
http://logging.apache.org/log4net/
From your question it is not quite clear if the log is displayed by the server or the client. However, log4net has support for logging over the net, e.g. an UPD Appender.

Alternatives ways to getting the properties of the file?

Background:
I have a file monitoring service that watches for changes in the files on the local system using the FileSystemWatcher class and i am handling for events like Created,Deleted,Renamed. When these events are triggered,I would simply want to GET THE PROPERTIES OF THE FILE such as FileName,FileSize,CreationTime,LastAccessTime,LastWriteTime using the FileSystemInfo class.
Problem:
While this service is running, I am unable to uninstall some programs for example (Microsoft Security Essentials). I have a feeling that these service is HANGING ON TO THE RESOURCES of the files marked for deletion because I can only uninstall those programs if only this service is running.
My Question is how can I GET THE PROPERTIES OF THE FILE (as specified above) in an ALTERNATIVE & efficient way without hanging on to the resources of the file ?
Here is my code using the FileSystemInfo
public void OnCreate/OnRenamed(object source, FileSystemEventArgs e)
{ FileInfo file = new FileInfo(e.FullPath);
String output = "<Event><TimeStamp>" + currentTime + "</TimeStamp>";
output += "<Name>" + action + "</Name>";
output += "<Properties><FileName>" + file.Name + "</FileName>";
output += "<FullPath>" + file.FullName + "</FullPath>";
output += "<FileSize>" + file.Length + "</FileSize>";
output += "<CreationTime>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.CreationTime) + "</CreationTime>";
output += "<LastAccess>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.LastAccessTime) + "</LastAccess>";
output += "<LastWriteTime>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.LastWriteTime = DateTime.Now) + "</LastWriteTime></Properties></Event>";
}
Sincerely,
Derek
Using FileSystemInfo will not normally 'hang on' to these files. You have to first figure out what exactly is causing other programs to stuck during uninstallation. Use ProcessMonitor to see what files are being accessed during uninstallation. The tool is pretty self explanatory, you need to filter on file system activity. Read this or google around.
Try to experiment by taking out one thing at a time. I assume these programs get uninstalled successfully when your service is not running. This proves that it is indeed your service that is causing issues. First comment your FileSystemInfo code. See if the problem goes away. Then comment out FileSystemWatcher and see if it helps.
Update: looks like this is offending line:
file.LastWriteTime = DateTime.Now
Try to comment this assignement and see if it solves the problem:
file.LastWriteTime /* = DateTime.Now */
Was this a typo or you really need to write LastWriteTime?
What you could do is every interval check for LastAccessTime and LastWriteTime and do some quick math based on the interval and you can determine if the file was last accessed or modified within the interval time. If it was, do what you have to do. Just an idea...

getting file name and moving it

string fName = Path.GetFileName(tempPaths[z]);
if (!File.Exists(subAch + fName))
{
File.Move(tempPaths[z], subAch + fName);
Console.WriteLine("moved!!! from " + tempPaths[z] + " tooooo ");
}
tempPaths is a list with all the image file paths. e.g. ./images/image4.jpg
subAch is a directory string.
I wish to get the file name of the file then move them to another directory. But with the code above i kept getting error: file is being used by other process.
Is there anyway which get the file name and move them? I have tried fileStream but was confused by it.
Please advice.
Thank you!
Your code should work just fine. You just need to figure out who is locking the files.
I'd put the code inside the if-block in a try-catch block to deal with the locked files.
I will also recommend you to use Path.Combine instead of dir + file.
One thing: you are checking if subAch + tempPaths[z] exists, yet you are copying to a different location; subAch + fName.
File is being used by another process means exactly that. Someone/something is already using the file, so can't move it. You can always catch the error and moving everything else?
I have use a non-ideal way to grab the file name and move the files to another place.
tempPaths.AddRange(Directory.GetFiles(rawStorePath, filter, SearchOption.AllDirectories));
The code above gets all the directories of all the files in the folder set. The outcome with be something like this. tempPaths is a List.
"./images/glass_numbers_5.jpg"
"./images/G.JPG"
"./images/E.JPG"
"./images/F.JPG"
"./images/glass_numbers_0.jpg"
"./images/C.JPG"
"./images/B.JPG"
"./images/A.JPG"
"./images/D.JPG"
"./images/glass_numbers_7.jpg"
then after i use a loop to grab the file names.
for (int i = 0; i < tempPaths.Count; i++)
{
//Getting the original names of the images
int pLength = rawStorePath.Length;
string something = tempPaths[i].Remove(0, pLength);
if (!_tfileName.ContainsKey(tempPaths[i]))
{ _tfileName.Add(tempPaths[i], something); }
}
rawStorePath is the path of the targeted path e.g.: ./images/
tempPath[i] e.g. : ./images/G.JPG
So with the length i remove the letters and get the file name back.
Please advice me for a ideal way to do this if there is any.
Thanks!

Categories

Resources