Developer forum

Forum » Development » Recommendations for logging from Scheduled Tasks

Recommendations for logging from Scheduled Tasks

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I am building my own scheduled task to send out shipping confirmation emails by inheriting BaseScheduledTaskAddIn. From this task, I would like to write detailed info to the log in case of failures, and a summary in case of success.

What is the recommended approach for logging? I can see that the base integration framework task seems to have a lot of logging setup already done, but not for BaseScheduledTaskAddIn.  Is there some guidance, or an existing class I can look at to get some inspiration? I want to make this as Dynamicweb-standard as possible.

Thanks,
Imar


Replies

 
Jonas Krarup Dam
Reply
This post has been marked as an answer

Hi Imar,

The DW  Visual Studio templates have an example scheduled task that you can refer to, for the logging implementation.

basically, the logging implementation has been done in the  BatchIntegrationScheduledTaskAddin baseclass which has the "extra" functionality that was added to scheduled tasks for data integration.
If you implement a scheduled task based on this class, instead of implementing it directly on the baseScheduledTaskAddin, you'll get the logging functionality for free.

Regards, Jonas
 

Votes for this answer: 1
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Imar,

Do you mean whether we have a logging API or whether there's a specific way logs should look in Dynamicweb?

We do have a logging API that we use internally in many places, like CheckoutHandlers and Email Marketing. The class to use it called Dynamicweb.LogToFile and you can find it in the Dynamicweb assembly.

Usage is pretty simple

  1. Provide a message to log
  2. Provide a location for your logs
  3. Specify how you want to logs to be stored and which additional information to include in the log
  4. Pass in any exception to log if you have one

You should be able to discern the usage from the method signature, LogToFile.Log, pretty easily.

Logs are stored in /Files/System/Log/<Your path>. If you store one log message per file, then they'll appear in /Files/System/Log/<Your path>/<Year>/<Month>.

The contents of the log files, e.g., the message you want to store, is completely up to you.

- Jeppe

EDIT

Please be aware that LogToFile does not work in a threading environment where HttpContext.Current does not exist. See this post: http://developer.dynamicweb.com/forum/development/logtofile-log-and-threading.aspx#Reply45253

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks guys, that's all very useful. However, it also shows the reason behind my initial question: there seem to be many different ways to log stuff ;-)

Just to name a few:

  1. Dynamicweb.LogToFile.Log(...); as suggested by Jeppe.
  2. Dynamicweb.Data.Providers.Logger.AddFileContentToLog as used in the integration framework. Seems to use Base.WriteTextFile under the hood.
  3. Dynamicweb.Information.Logger.LogGeneral: logs data in the GeneralLog table in the database.
  4. Dynamicweb.Ecommerce.LiveIntegration.Logger.Instance.Log()

In addition, I think there's a way to append logging information to an order in the backend and I am sure there are a few other ways to log data.

So, different ways to do similar things ;-)

My use cases:

1. Provide simple status information on an order under Ecommerce in the backend. Just like the checkout handler does, I would like to add additional messages to the order so a standard content manager can see them. Do I use the OrderDebuggingInfo class for that? I found the static Save method on that class which seems perfect for my scenario.

2. Provide low level data for developers to figure out what's going on. Dynamicweb.LogToFile.Log seems right, but I wonder if option #2 and #4 in the list above are better.

Thanks!

Imar

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

We are aware that there are too many ways to do the same tasks in Dynamicweb. We are actively taking steps to fix this in the next major version of Dynamicweb.

To respond directly to your questions:

  1. OrderDebuggingInfo.Save is what you want
  2. LogToFile.Log is what you want

I would not recommend using other logging APIs, unless you add your own. LogToFile has been battle tested in CartV2 and CheckoutHandler to provide both process logs and error logs. Be aware that the performance of LogToFile is directly proportional to the amount of extra information you add.

- Jeppe

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> We are aware that there are too many ways to do the same tasks in Dynamicweb. We are actively taking steps to fix this in the next major version of Dynamicweb

Yep, no worries. That's why I started this thread, to come to an agreement on the recommended approach for third party developers so we're not inventing yet another wheel (guilty as charged myself by adding my own versions of NLog + wrappers).

I'll use LogToFile.Log then.

Thanks guys!

Imar

 
Kim Søjborg Pedersen
Reply

Hi just an update: In DW9 LogToFile is moved to Dynamicweb.Logging.LogManager.Current.GetLogger("GetOrdersToCapture").Info("Log point reached");

 

You must be logged in to post in the forum