way to check values before Initialisation


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
 
Ad

Advertisements

M

Marc Gravell

Are you looking for a static ctor?

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

Marc
 
T

Terry Rogers

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
Ad

Advertisements

S

softwareakash

HI All

I found the solution by using static readonly values
Didnt have to create a static constructor

this is my code now

public class Logger
{
// Initialising variables from the config Files
private static readonly string FullFilePath =
GetFilePathConfigSetting("FilePath");

and I am checking the correct values in GetFilePathConfigSetting()
method

Thanks and Regards
Akash
http://www.mstechinterviews.blogspot.com /
http://www.dotnetforum.blogspot.com
 

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

Top