Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Best Method to create a log file

Tags:

c#

file

logging

I'm writing a tool that's going to be check the health of workstations across a network, and will fix according to the issues it finds. I want to create a log file as the app is running through its tasks / checks on each machine. I just want to get this working on a single machine for now, but in time it will be scanning 100+ machines in one go (Threaded out).

What is the best way to create a log file?

I was thinking of using a List<string> to build up the log file in memory and then output it to a file once it had finished.

I'm just thinking there may be a better way of doing this?

like image 333
Derek Avatar asked Apr 20 '12 07:04

Derek


People also ask

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

I would recommend log4net.

You would need multiple log files. So multiple file appenders. Plus you can create the file appenders dynamically.

Sample Code:

using log4net; using log4net.Appender; using log4net.Layout; using log4net.Repository.Hierarchy;  // Set the level for a named logger public static void SetLevel(string loggerName, string levelName) {     ILog log = LogManager.GetLogger(loggerName);     Logger l = (Logger)log.Logger;      l.Level = l.Hierarchy.LevelMap[levelName];     }  // Add an appender to a logger public static void AddAppender(string loggerName, IAppender appender) {     ILog log = LogManager.GetLogger(loggerName);     Logger l = (Logger)log.Logger;      l.AddAppender(appender); }  // Create a new file appender public static IAppender CreateFileAppender(string name, string fileName) {     FileAppender appender = new         FileAppender();     appender.Name = name;     appender.File = fileName;     appender.AppendToFile = true;      PatternLayout layout = new PatternLayout();     layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";     layout.ActivateOptions();      appender.Layout = layout;     appender.ActivateOptions();      return appender; }  // In order to set the level for a logger and add an appender reference you // can then use the following calls: SetLevel("Log4net.MainForm", "ALL"); AddAppender("Log4net.MainForm", CreateFileAppender("appenderName", "fileName.log"));  // repeat as desired 

Sources/Good links:

Log4Net: Programmatically specify multiple loggers (with multiple file appenders)

Adding appenders programmatically

How to configure log4net programmatically from scratch (no config)

Plus the log4net also allows to write into event log as well. Everything is configuration based, and the configuration can be loaded dynamically from xml at runtime as well.

Edit 2:

One way to switch log files on the fly: Log4Net configuration file supports environment variables:

Environment.SetEnvironmentVariable("log4netFileName", "MyApp.log"); 

and in the log4net config:

<param name="File" value="${log4netFileName}".log/> 
like image 172
Aseem Gautam Avatar answered Oct 11 '22 09:10

Aseem Gautam