C Sharp Test Question 6

You are developing a logging module for a large application by using the .NET Framework.

You need to append logging information to a file named application.log. This log file is opened when the application is started and is closed only when the application is closed. However, you append text several times to the file during a session.

You must minimize overhead to the logging process to ensure maximum performance.

Which code segment should you use to create the log file?

C Sharp Test Question 6

C Sharp Test Question 6

Question 6 Explanation:

If you are going to reuse an object several times, you should use the instance method of the FileInfo class instead of the corresponding static methods of the File class, because a security check will not always be necessary. This will minimize any overhead and improve the performance of the application.

You should use the following code to append data to the log file:

FileInfo fi = new FileInfo(@”c:\application.log”);
StreamWriter sw = fi.AppendText();

Using the following code is incorrect because you should use FileMode.Append only in conjunction with FileAccess.Write.

FileInfo fi = new FileInfo(@”c:\application.log”);
FileStream fs = fi.Open(FileMode.Append);

Objective: 
Implementing serialization and input/output functionality in a .NET Framework application

Sub-Objective:
4.4 Access files and folders by using the File System classes. (Refer System.IO namespace)