Runtime selection of FileName

Topics: Logging Application Block
Nov 26, 2009 at 3:10 AM

Hello All,

I am fairly new to the application blocks and the reason i wanted to use this is to 'Not reinvent the wheel'. I have a scenario which i dont seem to find a solution for, and any advice is much appreciated.

I have a ftp location where my clients transfer files to. Each client is given a specific location and each transfer is in a directory by itself. for ex.

FTP

   CLIENT 1

       BATCH 1

       BATCH 2

   CLIENT 2

        BATCH 1

etc.

I have a filesystem monitor that checks for the new in-coming file / folder and runs a specific program against it. The output of the processing is written into the respective folders as a log file. I thought i can change the filename property of the FlatfileTraceListener for each batch i pick up. Clearly i have misunderstood something. Can someone show me a way out?

 

thanks, Hari.

Nov 26, 2009 at 5:34 AM

Hi,

Have you came across this one: http://msdn.microsoft.com/en-us/library/dd203211.aspx

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2009 at 6:23 AM
Edited Nov 26, 2009 at 7:09 AM

Hi there! 

Thanks for the prompt reply. I did come across this link, but i presumed that the save method will alter the configuration file permanently.

ok, i tried the example. But i am not sure how to proceed using it for the implementation i am after. the pseudocode is as below. Any advice is much appreciated.

 

Class Batch holds an array list of new folders getting added.

Every 2 hours, i loop through the array list

for each folder in arraylist

     extract the data from the batch

     insert into db

     write the progress into a txt file within the folder... this is where i intend to say - listener.filename = folder; logger.write(...) or something like that.

next.

 

oh by the way, this runs as a windows service.

thanks, hari

Nov 27, 2009 at 2:34 AM

I'm not sure what exactly is your question now, are you asking for the code that changes the filename of the flat file?

LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

FlatFileTraceListenerData fileTraceListener = loggingSettings.TraceListeners.First(a => a.Name == "Flat File Trace Listener") as FlatFileTraceListenerData;

fileTraceListener.FileName = "newfilename.txt";

(entlibConfig is your instance of Configuration object)

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 30, 2009 at 3:52 AM

Thanks for your assistance with this. This was what i was after. I am faced with another issue in the same lines and i hope you can throw some light on it.

I modify the config file (with the new filename) and save it. But i noticed that the changes dont come into effect for some time. So the logs are written in a sequence that i do not prefer.

So the question is - Is there any way to automatically refresh the  config object after i perform the save? Are there any negative implications by doing this?

Nov 30, 2009 at 7:09 AM
Hi all, i think i found the solution to my problem at the url http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=30391 Using the environment variables, i can now dynamically change the filename for each of the batch files that are processed. in the flat file file trace listener, the file name is configured as C:\logs\%CIS_LOG_PATH%\log.log at runtime, Environment.SetEnvironmentVariable("CIS_LOG_PATH", Date.Today.Year & Date.Today.Month & Date.Today.Day, EnvironmentVariableTarget.Process) This should do the job. cheers, Hari.
Dec 1, 2009 at 6:01 AM

Hi All,

Just when i thought i have some light at the end of the tunnel, i have another problem.

In a loop i execute the following

Environment.SetEnvironmentVariable("CIS_APP_LOG_FILE_LOC", item.BatchLocation, EnvironmentVariableTarget.Process)

The filename for the listener is set to %CIS_APP_LOG_FILE_LOC%\log.log

The log files are being created in the right folder, but the file name never remains log.log.

 

The following are some examples of the file names i got. ANY idea why this is happening?

2d7fdb8a-1c6c-4d81-9eb4-a1a94688f1fdlog.log

8b9785b6-f4ff-406e-84b1-625c3c7f06c4log.log

09d6b489-fb45-41c6-9859-a75c5d34f9dclog.log

21cb07f2-4517-4ec3-92c1-e374f988283dlog.log

922a299d-40de-48a0-b948-10930e4928c6log.log

 

Somehow the '\' gets lost before the file name.

help!!!, Hari.

Dec 1, 2009 at 7:33 AM

Hi,

I usually encounter this one when I'm trying to log to a file that is still open. Can you try adding a

Logger.Writer.Dispose();

After the call to the Logger.Write.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 1, 2009 at 7:37 AM

You are absolutely right. The dispose worked.

Thanks, Hari.