Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a log to PLC Next log file #21

Open
MaltarIvan opened this issue Aug 20, 2024 · 4 comments
Open

Write a log to PLC Next log file #21

MaltarIvan opened this issue Aug 20, 2024 · 4 comments
Labels
feature-request New feature or request

Comments

@MaltarIvan
Copy link

One example of the feature that I find useful is writing a log to a PLC Next log file.

For example: if you catch an exception in your C# code, you could write the exception message to log file for diagnostic purposes.

Is there such possibility in PLC Next development tools?

@MaltarIvan MaltarIvan added the feature-request New feature or request label Aug 20, 2024
@martinboers
Copy link
Member

I think the Log class might help in this case:

image

This should log a message to the file /opt/plcnext/logs/Output.log.

Note that, by default, DEBUG and TRACE messages are not recorded in that log file. More information here:

https://plcnext.help/te/PLCnext_Runtime/output_log.htm

@MaltarIvan
Copy link
Author

Thank you for the answer. Is there maybe an example of using the methods in Log class? Specifically I am interested in how to define the format string and 'args' arguments.

@martinboers
Copy link
Member

It just uses standard C# formatting.

This example is based on the C# function template, and writes the value of the two input parameters to the log file:

   [Function]
   public static class Function1
   {
       [Execution]
       public static bool __Process(
           [Input] short IN1,
           [Input, DataType("WORD")] ushort IN2)
       {
           bool Function1 = false;

           if (IN1 + IN2 == 2)
               Function1 = true;

           Log.Info("Hello. IN1 = {0} and IN2 = {1}", IN1, IN2);

           return Function1;
       }
   }

There's resources online that explain C# output formatting, e.g.:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

@MaltarIvan
Copy link
Author

Ok, I understand now. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants