way to check values before Initialisation

  • Thread starter Thread starter softwareakash
  • Start date Start date
S

softwareakash

Hi

I have a dll which logs some values in a text file. In this I am
initialising some values as soon as the object gets called like this

public class Logger
{
// Initialising variables from the config Files
private static string FilePath =
ConfigurationSettings.AppSettings["FilePath"];
private static string FileSize =
ConfigurationSettings.AppSettings["MaxFileSizeKB"];
private static string Level =
ConfigurationSettings.AppSettings["LogLevel"];
// Initialising the Filestream and Streamwriter
private static FileStream fs = new FileStream(FilePath,
FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
private static StreamWriter sw = new StreamWriter(fs);

public Logger()
{

}


the problem is that I want to check whether the config file really
exists or the values are correct in the config file and also if the
Filepath is present or not.
If i check all this in the constructor than I cant use static keyword
anymore and I cant use these values in all functions of my dll.

other wise my whole purpose gets lost if Filestream tries to resolve a
Filepath which is not existing, it throws an exception

Can anyone tell me what can be the resolution of this
Regards
Akash
 
Are you looking for a static ctor?

public class SomeClass {
static SomeClass() {
// static ctor; static initialisation / validation code
}
}

Marc
 
softwareakash said:
Hi

I have a dll which logs some values in a text file. In this I am
initialising some values as soon as the object gets called like this

public class Logger
{
// Initialising variables from the config Files
private static string FilePath =
ConfigurationSettings.AppSettings["FilePath"];
private static string FileSize =
ConfigurationSettings.AppSettings["MaxFileSizeKB"];
private static string Level =
ConfigurationSettings.AppSettings["LogLevel"];
// Initialising the Filestream and Streamwriter
private static FileStream fs = new FileStream(FilePath,
FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
private static StreamWriter sw = new StreamWriter(fs);

public Logger()
{

}


the problem is that I want to check whether the config file really
exists or the values are correct in the config file and also if the
Filepath is present or not.
If i check all this in the constructor than I cant use static keyword
anymore and I cant use these values in all functions of my dll.

other wise my whole purpose gets lost if Filestream tries to resolve a
Filepath which is not existing, it throws an exception

Can anyone tell me what can be the resolution of this
Regards
Akash

You want a static constructor:
public class Logger
{
static Logger()
{
// Add static construction logic here
}
}

hth
Terry
 
Hi,

A few things to note,
First your current problems can be solved by using a static constructor.

Next I do not like the idea of having the log file open during the entire
live of the app, IMO it's better to just open it when you need to write and
close it right away.
Of course this open/write operation should be enclosed in a try/catch block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top