write xml format log file - c#

Our customized logging sysem saves info into a xml file, like this:
<?xml version="1.0" standalone="yes"?>
<log>
<entry> msg </entry>
<entry> msg </entry>
.
.
.
</log>
Right now we are using StreamWriter to write. If the system crashes (we are in the middle of the development), the end tag/element, </log>, will not be written to the file so the xml file cannot be opened correct with IE. I found there are two possible options to correct this:
Option A: write the end tag at very beginning when file is initialized and then do "insert" for each new entry. right now we are doing "append".
Option B: change to XmlDocument or XmlWriter.
In option A, change "append" to "insert", we need to keep the current location, which is right before the end tag, </log>,. And write from that current location. I didn't find a good way to do this.
In option B, the XmlWriter is almost the same as other stream/writer. No easy way to "insert". The XmlDocument is easy to "insert", but there is no Flush() method to call. we have to Save() to make it write to file. If the Save() means close the stream/file, I don't want to do Load() and Save() for each entry. that will be too expensive, I think.
do you have any good idea about this?

I would set a global exception handler: that way you could finalize the log, but most important you can collect details relevant to the failure (a stacktrace but also memory state, etc.). In my experience this has always been very effective to understand crashes later on when the system is running "in the wild".

You can use the Enterprise Library that allow you to log to a XML file and much much more. I strongly recomend that you, at least, take a look at that.
But if you just want to write a XML log without the other goods from EntLib you can try log4Net for example.

You might want to think about using try ... catch ... finally when writing the log file(s). Then if the main method (for writing the logs) fails, inside of the finally switch to a different method and/or location (if needed) and continue writing the log.

You may want to consider implementing an unhandled exception filter in your application and take care of cleanly flushing your logs there. Take a look at this blog post for information.

Are you sure about using xml for logging?
You can try html for you purposes.
<title>Cool log</title>
<pre>Started</pre>
<pre>Finished</pre>

Related

XML Edit to file using C#

I am working on a WPF application and I have a simple XML file that I am parsing using 'XmlDocument' and is working fine for the readinh part.
I want the use to be able to add, edit or delete any node and save these changes to the file.
I tried using 'XElement' but it seems to change the instance itself and not the file.
My XML file looks something like this:
<Configuration>
<A_0.04_5>
<ML407Configuration>
<AM_Amp>10</AM_Amp>
<AMRJ_Amp>10</AMRJ_Amp>
<FM_Freq>20</FM_Freq>
<FM_Phase_Shift>20</FM_Phase_Shift>
</ML407Configuration>
<BertConfiguration>
<BERT_LR>25.78125</BERT_LR>
<BERT_PRBS>7</BERT_PRBS>
<BERT_Scaling>1000</BERT_Scaling>
</BertConfiguration>
</A_0.04_5>
<B_1.333_0.15>
<ML407Configuration>
<AM_Amp>10</AM_Amp>
<AMRJ_Amp>10</AMRJ_Amp>
<FM_Freq>20</FM_Freq>
<FM_Phase_Shift>20</FM_Phase_Shift>
</ML407Configuration>
<BertConfiguration>
<BERT_LR>25.78125</BERT_LR>
<BERT_PRBS>7</BERT_PRBS>
</BertConfiguration>
</B_1.333_0.15>
<C_4_0.05>
<ML407Configuration>
<BUJ_LR>25</BUJ_LR>
<BUJ_Pattern>7</BUJ_Pattern>
<PM_BUJ_Amp>7</PM_BUJ_Amp>
<BUJ_Amp>80</BUJ_Amp>
</ML407Configuration>
<BertConfiguration>
<BERT_LR>25.78125</BERT_LR>
<BERT_PRBS>7</BERT_PRBS>
</BertConfiguration>
</C_4_0.05>
</Configuration>
What I tried is the following:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/Configuration.xml";
XElement xml = XElement.Load(filePath);
// This seems to remove the node from xml instance and not from the file
// Should I save the file again or is there another way to do it
// Same applies for add and edit
xml.Elements("C_4_0.05").Remove();
I have seen a lot of similar questions but I don't know if any of them change directly to the file or not
XElement.Load loads an XML structure from a file into memory. Any changes you make to that structure are also done in memory. If you want to write those changes back to a file (technically called serialization) you need to call XElement.Save.

Simple c# to put and get a single log line in a AWS bucket

So, we have a bucket that's been created and has files in it, but we need to log whenever files are added, removed, or viewed.
I'm looking for something simple, such as:
using (s3Client = new AmazonS3Client("accessKeyId", "secretAccessKey", "sessionToken", bucketRegion)) {
// log an entry here--"New file added! Filename: 'foo.gif'"
// get all log entries in the past day }
Is this doable or is that a lot of other work surrounding it? I've success fully used GetBucketLoggingRequest against this bucket, but it's just returning what looks to be an encoded string and not what I'd expect.
I've read that Console.Write and Writeline automatically log and that's fine for our sample console app but we won't be using those in our web api.
Hopefully I'm just missing something simple, most of the examples I can find are Java/E2 which don't do me much good. Thanks!

What could cause an XML file to be filled with null characters?

This is a tricky question. I suspect it will require some advanced knowledge of file systems to answer.
I have a WPF application, "App1," targeting .NET framework 4.0. It has a Settings.settings file that generates a standard App1.exe.config file where default settings are stored. When the user modifies settings, the modifications go in AppData\Roaming\MyCompany\App1\X.X.0.0\user.config. This is all standard .NET behavior. However, on occasion, we've discovered that the user.config file on a customer's machine isn't what it's supposed to be, which causes the application to crash.
The problem looks like this: user.config is about the size it should be if it were filled with XML, but instead of XML it's just a bunch of NUL characters. It's character 0 repeated over and over again. We have no information about what had occurred leading up to this file modification.
We can fix that problem on a customer's device if we just delete user.config because the Common Language Runtime will just generate a new one. They'll lose the changes they've made to the settings, but the changes can be made again.
However, I've encountered this problem in another WPF application, "App2," with another XML file, info.xml. This time it's different because the file is generated by my own code rather than by the CLR. The common themes are that both are C# WPF applications, both are XML files, and in both cases we are completely unable to reproduce the problem in our testing. Could this have something to do with the way C# applications interact with XML files or files in general?
Not only can we not reproduce the problem in our current applications, but I can't even reproduce the problem by writing custom code that generates errors on purpose. I can't find a single XML serialization error or file access error that results in a file that's filled with nulls. So what could be going on?
App1 accesses user.config by calling Upgrade() and Save() and by getting and setting the properties. For example:
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}
App2 accesses info.xml by serializing and deserializing the XML:
public Info Deserialize(string xmlFile)
{
if (File.Exists(xmlFile) == false)
{
return null;
}
XmlSerializer xmlReadSerializer = new XmlSerializer(typeof(Info));
Info overview = null;
using (StreamReader file = new StreamReader(xmlFile))
{
overview = (Info)xmlReadSerializer.Deserialize(file);
file.Close();
}
return overview;
}
public void Serialize(Info infoObject, string fileName)
{
XmlSerializer writer = new XmlSerializer(typeof(Info));
using (StreamWriter fileWrite = new StreamWriter(fileName))
{
writer.Serialize(fileWrite, infoObject);
fileWrite.Close();
}
}
We've encountered the problem on both Windows 7 and Windows 10. When researching the problem, I came across this post where the same XML problem was encountered in Windows 8.1: Saved files sometime only contains NUL-characters
Is there something I could change in my code to prevent this, or is the problem too deep within the behavior of .NET?
It seems to me that there are three possibilities:
The CLR is writing null characters to the XML files.
The file's memory address pointer gets switched to another location without moving the file contents.
The file system attempts to move the file to another memory address and the file contents get moved but the pointer doesn't get updated.
I feel like 2 and 3 are more likely than 1. This is why I said it may require advanced knowledge of file systems.
I would greatly appreciate any information that might help me reproduce, fix, or work around the problem. Thank you!
It's well known that this can happen if there is power loss. This occurs after a cached write that extends a file (it can be a new or existing file), and power loss occurs shortly thereafter. In this scenario the file has 3 expected possible states when the machine comes back up:
1) The file doesn't exist at all or has its original length, as if the write never happened.
2) The file has the expected length as if the write happened, but the data is zeros.
3) The file has the expected length and the correct data that was written.
State 2 is what you are describing. It occurs because when you do the cached write, NTFS initially just extends the file size accordingly but leaves VDL (valid data length) untouched. Data beyond VDL always reads back as zeros. The data you were intending to write is sitting in memory in the file cache. It will eventually get written to disk, usually within a few seconds, and following that VDL will get advanced on disk to reflect the data written. If power loss occurs before the data is written or before VDL gets increased, you will end up in state 2.
This is fairly easy to repro, for example by copying a file (the copy engine uses cached writes), and then immediately pulling the power plug on your computer.
I had a similar problem and I was able to trace my problem to corrupted HDD.
Description of my problem (all related informations):
Disk attached to mainboard (SATA):
SSD (system),
3 * HDD.
One of the HDD's had a bad blocks and there were even problems reading the disk structure (directories and file listing).
Operation system: Windows 7 x64
file system (on all disks): NTFS
When the system tried to read or write to the corrupted disk (user request or automatic scan or any other reason) and the attempt failed, all write operations (to other disk's) were incorrect. The files created on system disk (mostly configuration files by another applications) were written and were valid (probably because the files were cashed in RAM) on direct check of file content.
Unfortunately, after a restart, all the files (written after the failed write/read access on corrupted drive) had the correct size, but the content of the files was 'zero byte' (exactly like in your case).
Try rule out hardware related problems. You can try to check 'copy' the file (after a change) to a different machine (upload to web/ftp). Or try to save specific content to a fixed file. When the check file on different will be correct, or when the fixed content file will be 'empty', the reason is probably on local machine. Try to change HW components, or reinstall the system.
There is no documented reason for this behavior, as this is happening to users but nobody can tell the origin of this odd conditions.
It might be CLR problem, although this is a very unlikely, the CLR doesn't just write null characters and XML document cannot contain null characters if there's no xsi:nil defined for the nodes.
Anyway, the only documented way to fix this is to delete the corrupted file using this line of code:
try
{
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
catch (ConfigurationErrorsException ex)
{
string filename = ex.Filename;
_logger.Error(ex, "Cannot open config file");
if (File.Exists(filename) == true)
{
_logger.Error("Config file {0} content:\n{1}", filename, File.ReadAllText(filename));
File.Delete(filename);
_logger.Error("Config file deleted");
Properties.Settings.Default.Upgrade();
// Properties.Settings.Default.Reload();
// you could optionally restart the app instead
}
else
{
_logger.Error("Config file {0} does not exist", filename);
}
}
It will restore the user.config using the Properties.Settings.Default.Upgrade();
again without null values.
I ran into a similar issue but it was on a server. The server restarted while a program was writing to a file which caused the file to contain all null characters and become unusable to the program writing/reading from it.
So the file looked like this:
The logs showed that the server restarted:
The corrupted file showed that it was last updated at the time of the restart:
I have the same problem, there is an extra "NUL" character at the end of serialized xml file:
I am using XMLWriter like this:
using (var stringWriter = new Utf8StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, IndentChars = "\t", NewLineChars = "\r\n", NewLineHandling = NewLineHandling.Replace }))
{
xmlSerializer.Serialize(xmlWriter, data, nameSpaces);
xml = stringWriter.ToString();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
if (removeEmptyNodes)
{
RemoveEmptyNodes(xmlDocument);
}
xml = xmlDocument.InnerXml;
}
}

Reading and writing XML

I am working on a C# and Android library project. What I am basically trying to do is write a library in Android that will send me crash details onto my server.
I then have a C# console application that runs on my server and processes the data received by Android and from this data I want to generate an XML file, so that another program can read in the XML file and provide a monthly report.
I've got stuck with the best way of writing and reading the XML though.
I've read a lot about it and found various things such as XMLWriter or XMLSerializer but I don't know which works best, nor do I understand entirely how these are implemented.
Below is a basic design of how the XML file should be written, this is what I've written manually to give an understanding of what I want to achieve.
<?xml version="1.0" encoding="utf-8" ?>
<apps>
<app>
<MyApp>
<appID>1</appID>
<applicationID>0027598641</applicationID>
<platform>Android</platform>
<CrashDetails>
<Exceptions>
<Exception>
<CrashID>55</CrashID>
<ExceptionType>NullPointerException</ExceptionType>
<FullException>NullPointerException at line 2</FullException>
<StartDate>01-11-2013 09:52:00</StartDate>
<EndDate>02-11-2013 14:43:13</EndDate>
<AppVersionName>6.1.1.6</AppVersionName>
<stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
<Severity>Critical</Severity>
<OccurrenceCount>9</OccurrenceCount>
</Exception>
<Exception>
<CrashID>56</CrashID>
<ExceptionType>NullPointerException</ExceptionType>
<FullException>NullPointerException at line 2</FullException>
<StartDate>01-11-2013 09:52:00</StartDate>
<EndDate>02-11-2013 14:43:13</EndDate>
<AppVersionName>6.1.1.6</AppVersionName>
<stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
<Severity>Critical</Severity>
<OccurrenceCount>9</OccurrenceCount>
</Exception>
</Exceptions>
</CrashDetails>
</MyApp>
<MyApp1>
<appID>2</appID>
<applicationID>4844354</applicationID>
<platform>Android</platform>
<CrashDetails>
<Exceptions>
<Exception>
<CrashID>55</CrashID>
<ExceptionType>NullPointerException</ExceptionType>
<FullException>NullPointerException at line 2</FullException>
<StartDate>01-11-2013 09:52:00</StartDate>
<EndDate>02-11-2013 14:43:13</EndDate>
<AppVersionName>6.1.1.6</AppVersionName>
<stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
<Severity>Critical</Severity>
<OccurrenceCount>9</OccurrenceCount>
</Exception>
</Exceptions>
</CrashDetails>
</MyApp1>
</app>
</apps>
Thanks for any help you can provide.
Our team typically uses LINQ to XML, which provides a really powerful way to work with XML data (including loading XML from files, parsing XML streams, creating XML document and writing/saving XML to files.)
The following link provides a good overview of LINQ to XML
http://www.dreamincode.net/forums/topic/218979-linq-to-xml/
In addition, you may find “the XML part” section of the following page helpful
http://www.codeproject.com/Articles/24376/LINQ-to-XML
Regards
Personally, I prefer to use LINQ to XML for any XML related stuff in C#. You can create XML Trees, and then persist/serialize those trees to file, XmlWriter, or other types of streams.
But in cases when I need huge performance, I prefer to create XML using StringBuilder class and string concatenation operations.

How to Create A file on Disk

i am working on a pc 192.168.2.200
i have made a simple C# Windows application on 192.168.2.200
I want to Create a Text File on D: of a Network PC whose IP is 192.168.2.201 and UserName is abc and Password is 123 by C# code in Windows Application
How i Will Create file on Network PC
any one can Help me
System.IO.File.Create(#"D:\myfile.txt");
OR
System.IO.File.WriteAllText(#"D:\myfile.txt","Hello this is my File");
Windows application or not, you'd use the same technique. If you don't care what the format is, but want to use it as an input file that you read/write from, then you might want to look into XmlSerializer, along with a custom class that you write (with username/password/IP properties).
Here is a simple tutorial on XML serialization:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
If you want a custom format, and only want to write to it (such as a log file), then you can do this:
var file = System.IO.File.Create("file.txt");
var writer = new System.IO.StreamWriter(file);
writer.WriteLine("fjkldsaf");
Or use the overload for StreamWriter that takes a filename:
var writer = new System.IO.StreamWriter("otherfile.txt");
writer.WriteLine("some text");
Keep in mind that writing a password in clear text onto your hard drive is not very secure (same with clear-text over the network, though I know you're not asking about that).
Make sure you either call Dispose or Close on your file streams when you're done with them. You can stick them in a using block to do this automatically (even if an exception is accidentally thrown in your file writing code).
using(var writer = new System.IO.StreamWriter("otherfile.txt"))
{
writer.WriteLine("some text");
}

Categories

Resources