Chg destination log file at runtime

Home Forums CodeSite Chg destination log file at runtime

Viewing 2 reply threads
  • Author
    Posts
    • #595

      Good morning,

      I’ve recently started evaluating CodeSite Express (Delphi 10.1). Getting it up and running was very straight forward. I am currently logging to a file but have a need to change the file path at runtime based on a message from another process.

      For instance, logging starts in Folder 1, and then upon receipt of a message from another process, we wish to have the target log file change to “Folder 2”. I’ve not found a way to change the log destination after logging starts. Logging continues posting in “Folder 1” even after changing the destination logfile path. I’ve tried a few combinations of setting active false, changing path, setting active true and such but have not happened across the proper sequence.

      Is functionality possible?

      Thank you in advance for your thoughts.

    • #596
      Ray Konopka
      Keymaster

        Hi Rich,

        Yes, CodeSite does support dynamically changing the log file. I suspect that you are changing the original TCodeSiteDestination object when you change the FilePath and not the CodeSiteManager’s DefaultDestination property (or an individual logger’s Destination property).

        The CodeSiteManager.DefaultDestination property is not a component reference property. It is more like the Items property of a ListBox. You can assign a TStringList to ListBox1.Items to populate the list box. But if you change the contents of the string list, the list box does not get updated. Here is some sample code that should help illustrate the situation:

        uses
          CodeSiteLogging;
        
        procedure TForm1.FormCreate(Sender: TObject);
        var
          Dest: TCodeSiteDestination;
        begin
          Dest := TCodeSiteDestination.Create( Self );
          Dest.LogFile.Active := True;
          Dest.LogFile.FilePath := 'C:\Temp';
          Dest.LogFile.FileName := 'Sample.csl';
        
          CodeSiteManager.DefaultDestination := Dest;
        end;
        
        procedure TForm1.btnSampleMessageClick(Sender: TObject);
        begin
          CodeSite.Send( 'Sample Message' );
        end;
        
        procedure TForm1.btnChangeLogFileClick(Sender: TObject);
        begin
          CodeSiteManager.DefaultDestination.LogFile.FileName := 'NewLogFile.csl';
        end;

        The form simply has two TButtons dropped on to it. At the start the DefaultDestination is set to log all messages to the Sample.csl file. Clicking the btnSampleMessage button simply sends a simple CodeSite message. The messages will be logged to the Sample.csl file.

        When the btnChangeLogFile button is clicked, the LogFile.FileName property of the CodeSiteManager.DefaultDestination is changed. Now, when the btnSampleMessage button is pressed, the CodeSite message will be sent to the NewLogFile.csl file.

        As an alternative, you could create a new CodeSiteDestination object with the updated FilePath and then assign that to the DefaultDestination property.

        Ray

      • #599

        Thank you Ray.

        Your interpretation of what I was doing was spot on.

        Your example made everything work perfectly.

        Happy Holidays!

    Viewing 2 reply threads
    • You must be logged in to reply to this topic.