Developer forum

Forum » Development » LogToFile.Log and threading

LogToFile.Log and threading

Gaetan Di Caro
Reply

Hello,

I'm having some trouble with LogToFile.Log since I upgraded to 8.8.

I have a custom module in which I execute some code. In this code I use tasks. Outside of a task I can log fine, no problem. However if I do that :

Task taskB = Task.Factory.StartNew(() => myFunction())
.ContinueWith(task =>
 {

LogToFile.Log("test", "MyFile", LogToFile.LogType.ManyEntriesPerFile);

});

 

Then I get a nullreferenceexception. Here is the stack trace :

   at Dynamicweb.LogToFile.GetFileName(LogType logType, String baseFolder)
   at Dynamicweb.LogToFile.Log(String message, Exception logException, String folder, LogType newLogFile, Boolean useExtendedPropertyLogging, LogElements logElements, Boolean disable71Logging)
   at Dynamicweb.LogToFile.Log(String message, Exception logException, String folder, LogType newLogFile, Boolean useExtendedPropertyLogging, LogElements logElements)
   at Dynamicweb.LogToFile.Log(String message, String folder, LogType newLogFile)
   at (my module).<>c__DisplayClass3.<GetContent>b__0(Task`1 task) in (my module path)
   at System.Threading.Tasks.Task.Execute()


Replies

 
Nicolai Høeg Pedersen
Reply

Hm, there has been a minor change on this piece of code when clustering is enabled. Instead of using the name of the IIS instance name for mapping the log file, it now uses the machine name.

But if you are not using NLB there should be no changes.

Do you use NLB on this solution?

 
Gaetan Di Caro
Reply

No indeed I'm not using load balancing, for now this solution is only on my local machine.

 
Nicolai Høeg Pedersen
Reply

Do you have NLB node in management center under web & http?

 
Gaetan Di Caro
Reply

I do yes. There's a list of inactive machines, the only active one is my ow, on top of the list.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi guys,

This is caused the HttpContext.Current being null in a background thread. When you spin up a new background thread, you need to set the HttpContext.Current in order to use some parts of the Dynamicweb API. In this case specifically, the code that fails is most likely HttpContext.Current.Server.MapPath.

You can pass in HttpContext.Current from you UI thread and set it inside the background thread, though it's risky as the object might be disposed if the UI thread finished before the background thread, which often is expected behavior. Unfortunately, the best solution for you is to call LogToFile from the UI thread and not try to make it asynchronous.

We are working to fix this in the next major version of Dynamicweb but because it's a major change throughout Dynamicweb, we'll probably not change this for the Dynamicweb 8 branch.

- Jeppe

 
Gaetan Di Caro
Reply

Alright, thanks for the explanation. It's not critical for now, I can log another way.

 
Nicolai Høeg Pedersen
Reply

And you can not install NLB module...

 

You must be logged in to post in the forum