Take the 2-minute tour ×
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It's 100% free, no registration required.

I am firing Component save event in SDL Tridion 2011. I have written code like below,

    public EventSystem()
    {
        EventHelper.WriteEventToHTMLLog("S-Constructor");
        Subscribe();
        EventHelper.WriteEventToHTMLLog("E-Constructor");
    }

    public void Subscribe()
    {
        EventHelper.WriteEventToHTMLLog("S-Subscribe");
        EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePre, EventPhases.Initiated);
        EventHelper.WriteEventToHTMLLog("E-Subscribe");
    }

    private void OnComponentSavePre(Component comp, SaveEventArgs args, EventPhases phase)
    {
        EventHelper.WriteEventToHTMLLog("Component save event is fired.");           
    }

In above code, EventHelper.WriteEventToHTMLLog writes string logs to physical file on server which is object of below class.

public class EventHelper
{
    public static void WriteEventToHTMLLog(String strMessage)
    {
        try
        {
            string dirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            dirPath = @"F:\Tridion\web\RREvent_SystemLogs\RR_EventSystem\";

            string FileContent = string.Empty;
            string updatedLogs = string.Empty;
            updatedLogs = System.DateTime.Now.ToString("HH : mm : ss ") + " = " + strMessage;

            if (!Directory.Exists(dirPath + @"\EventLogsComponent"))
            {
                Directory.CreateDirectory(dirPath + @"\EventLogsComponent");
            }

            string FileName = "Log_" + System.DateTime.Now.ToString("dd_MM_yyyy") + ".xml";
            FileInfo fileInfo = new FileInfo(dirPath + @"\EventLogsComponent\" + FileName);

            if (fileInfo.Exists)
            {

                FileContent = File.ReadAllText(fileInfo.FullName);
                FileContent = FileContent.Replace("</Logs>", "<Log>" + updatedLogs + "</Log>" + "</Logs>");

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(FileContent);
                xDoc.Save(fileInfo.FullName);
            }
            else
            {
                FileContent = "<Logs><Log>" + updatedLogs + "</Log></Logs>";

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(FileContent);
                xDoc.Save(fileInfo.FullName);
            }


        }
        catch (Exception e3)
        {

        }
    }
}

Here, I am facing weird problem, When I have fired event very first time, all the logs are written successfully. But when I have uploaded dll second time ( with some changes ), event code is reflected on User Interface properly but new logs are not generated of the event method. ( only subscribe method & constructor logs are written ). And when I have uploaded dll third time, no event log is written in file of any method.

I am confused. Please suggest what would be the cause behind this problem ?

Thanks.

share|improve this question
1  
as you mentioned, after the code changes it is not logging. So I would suggest you to save your code and roll back to your original code and check if it is working. If it’s working after roll back means there is something wrong with the code changes otherwise it’s something else. Also, please restart the CM (com+/service) after deploying the new dll. –  Saurabh Gangwar Mar 18 at 7:15
4  
You really shouldn't swallow all exceptions :) Remove the try/catch for now and I bet you will get clearer errors that tell you what is going wrong. If you do decide to add the try/catch again, you should catch specific exceptions instead of all of them. –  Peter Kjaer Mar 18 at 7:17
1  
Adding to my previous comment, you can also use "log4net" for logging; if you don't have any specific requirement having your own logger. –  Saurabh Gangwar Mar 18 at 7:38

1 Answer 1

up vote 5 down vote accepted

Even though the Tridion.Logging namespace is officially not part of the public API, I still would recommend using it from your event handlers. It allows you for a clean and easy way of adding messages to the Tridion event log on Windows, and you don't need to write additional code or add an additional logging framework.

Now important to note is that since this is officially not part of the public API, it might get changed in a future release without notice. So upon upgrades you should always check if your event handler still works as expected.

Simple example of how to log exceptions:

using Tridion.Logging;

try
{
    // your code that can throw an exception
}
catch (Exception ex)
{
    // just log whatever went wrong
    // please note it is always better to catch a specific exception and handle it
    Logger.Write(ex, "Event Name", LoggingCategory.General, TraceEventType.Error);
}

// or just log an informational message
Logger.Write("Hello World!", "Event Name", LoggingCategory.General, TraceEventType.Information);

For this to work you have to add a reference to the Tridion.Logging.dll file which can be found in the {TRIDION_HOME}\bin\client directory on your content management server.

If you want a future proof solution and have the option to configure logging yourself, then check out the answers here What is the best way to do logging from the Tridion Custom Events c# code?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.