Reputation: 2855
I want to log the status in my app and I want to avoid passing TextWriter parameter to methods e.g.
public static void M1(TextWriter w)
down to all methods that I call. Anyway to avoid doing this? I want something like the following but it errors with: The name 'w' does not exist in the current context
Anyway to make the instance of streamwriter available to all methods? Or something like that? If possible can you provide an example.
public static void Main(string[] args)
{
using (StreamWriter w = File.CreateText(ConfigManager.logFile))
{
Log("start");
M1();
Log("end");
}
}
public static void M1()
{
Log("M1 start");
Log("M1 end");
}
public static void Log(string logMessage)
{
w.WriteLine("[{0}][{1}] {2} ", String.Format("{0:yyyyMMdd HH:mm:ss}", DateTime.Now),
"Info", logMessage);
w.Flush();
}
Upvotes: 1
Views: 6059
Reputation: 5033
First of all, you might want to have a look at log4net or NLog, 2 logging frameworks that can provide a lot of functionality and prevent a lot of problems concerning logging.
These frameworks are not hard to use, and you can get up and running pretty quickly. However, if you want a quick and simple solution, as in your request:
w
is not known in that scope. There's a few ways to tackle this, but in your situation I'd suggest the following:
Create a class that does logging for you:
public class Logger
{
public void Log(string message)
{
using (StreamWriter w = File.CreateText(ConfigManager.logFile))
{
w.WriteLine("[{0}][{1}] {2} ", String.Format("{0:yyyyMMdd HH:mm:ss}", DateTime.Now), "Info", logMessage);
w.Flush();
}
}
}
Then, just call that class. You can have a (static) global variable in your static Program class, or create a new instance and call it each time.
Static, define in your Program class, outside of any functions:
static Logger logger = new Logger();
Instance, just define on-the-spot where necessary:
var logger = new Logger()
Upvotes: 6
Reputation: 10359
If you need to use your StreamWriter in different classes, you have options :
make them inherit from a LoggingClass with your StreamWriter defined in it
create a static Logger class, with your static SW, and with a static method Log (better way imho)
public class Logger { private static StreamWriter w; public void Log(string message) { if(w == null) w = File.CreateText(ConfigManager.logFile); { w.WriteLine("[{0}][{1}] {2} ", String.Format("{0:yyyyMMdd HH:mm:ss}", DateTime.Now), "Info", logMessage); w.Flush(); } } }
Upvotes: 0
Reputation: 1585
If you are using "using statement" to create a stream write then it will not be available outside the scope of it. msdn
Using(StreamWriter sw= new StreamWriter(filename))
{
sw.Writeline("error");
}
If you want to use stream as a global variable try to declare sw as a class memeber. Note: if using as global variable remember to close the streamwriter.
in your case it would be like this
private static StreamWriter w;
public static void Main(string[] args)
{
w = File.CreateText(ConfigManager.logFile);
Log("start");
M1();
Log("end");
}
public static void M1()
{
Log("M1 start");
Log("M1 end");
}
public static void Log(string logMessage)
{
w.WriteLine("[{0}][{1}] {2} ", String.Format("{0:yyyyMMdd HH:mm:ss}", DateTime.Now),
"Info", logMessage);
w.Flush();
}
Upvotes: 0