Reputation: 47733
I cannot figure out why I keep getting a null ref on filename when I'm clearly calling this singleton and it should be calling the Logger() to set the filename variable:
public class Logger
{
private static Logger defaultLogger = null;
readonly string filename;
public static Logger DefaultLogger
{
get
{
// Check for valid instance
if (defaultLogger == null)
defaultLogger = new Logger();
// Return instance
return defaultLogger;
}
}
private Logger()
{
filename = ConfigurationManager.AppSettings["MyLogPath"];
}
public string Filename
{
get { return this.filename; }
}
public void Write(EntryType type, string data)
{
lock (this)
{
using (StreamWriter writer = new StreamWriter(filename, true))
{
//do something
}
}
}
}
Here's how I'm calling this class:
Logger.DefaultLogger.Write(EntryType.Error, e.ToString());
So I get this error during runtime saying that filename is null:
Value cannot be null.
Parameter name: path
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: path
Source Error:
Line 49:
Line 50:
Line 51: public string Filename
Line 52: {
Line 53: get { return this.filename; }
This was our original code (I did not write this), same problem:
public class Logger
{
public static Logger DefaultLogger = new Logger();
string filename;
public Logger()
{
filename = ConfigurationManager.AppSettings["LogPath"];
}
public Logger(string filename)
{
this.filename = filename;
}
public string Filename
{
get { return this.filename; }
}
public void Write(LogEntryType type, string data)
{
lock ()
{
using (StreamWriter writer = new StreamWriter(filename, true))
{
...
}
}
}
Upvotes: 2
Views: 3655
Reputation: 83254
Is it because
ConfigurationManager.AppSettings["MyLogPath"];
is null?
Check that your App.Config
file is there ( it takes the form of yourexe.exe.config
in your bin folder). You App.Config should have the following lines:
<configuration>
<appSettings>
<add key="MyLogPath" value="C:\Simple.txt" />
<appSettings>
</configuration>
Maybe in order to test, you can temporary set the filename to a well-know path ( filename=@"D:\C#\mytext.txt";
) and see whether you get the error or not.
If I set the filename explicitly, then I won't have such an error, OTOH,if I set
filename=ConfigurationManager.AppSettings["MyLogPath"];
then I will get a
System.ArgumentNullException : Value cannot be null. Parameter name: path at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean append)
If I stepped using a debugger, I can see that it failed at:
(StreamWriter writer = new StreamWriter(filename, true))
I don't know why your debugger won't hit the constructor. My debugger hit the constructor in both cases.
Upvotes: 6
Reputation: 869
Try assigning your Logger to instance variable in your client code and accessing it's FileName. Perhaps it's not getting assigned correctly.
Also, do you need to have filename as readonly if you're only providing a get property?
Upvotes: 0
Reputation: 273179
I copy pasted the code and replaced
filename = ConfigurationManager.AppSettings["MyLogPath"];
with
filename = @"test.log";
And it works fine. So your error is in the spelling of "MyLogPath"
or in the app.config
.
Upvotes: 1
Reputation: 14675
You're using a lock, so you expect this to be called in a multithreaded scenario, but you don't synchronize DefaultLogger. Also, are you sure you're setting fileName to something non-null in the first place?
Upvotes: 0
Reputation: 7154
Static initilaizers are executed when the class definition is first accessed, which in your case means they are executed before your private constructor.
Upvotes: 0
Reputation: 11701
Without looking into it too much..
private Logger()
{
filename = ConfigurationManager.AppSettings["MyLogPath"];
throw new Exception("filename = "+filename);
}
Does the exception get thrown?
Upvotes: 1