By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,184 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,184 IT Pros & Developers. It's quick & easy.

way to check values before Initialisation

P: n/a
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

Aug 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
softwareakash wrote:
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

Aug 7 '06 #2

P: n/a
Are you looking for a static ctor?

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

Marc
Aug 7 '06 #3

P: n/a
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.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"softwareakash" <so***********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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

Aug 7 '06 #4

P: n/a
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

Aug 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.