To Write to the Trace Log from the Code
When you are developing and troubleshooting the customization code, you may need to know what happens during code execution (for example, an action being called or an event being raised). You can write information to the trace log directly from your code by using one of the following approaches:
- Using the methods of the ILogger<T> interface, which is provided by Microsoft
- Using the methods of the PXTrace static class
To save the trace logs of all subsessions of all users in a single file on the server, a user with the Administrator role has to change the web.config file of the MYOB Acumatica instance to configure the appropriate trace provider. See the Trace section of Using Logs for details.
A user can see the log message in the trace log if the user clicks
on the form title bar after the corresponding logic has been executed.Writing to the Trace Log by Using the Methods of ILogger<T>
MYOB Acumatica supports the logging mechanism provided by the Microsoft.Extensions.Logging.Abstractions NuGet package. You can use this mechanism in custom code to write to the trace log.
To use this mechanism, you should do the following:
- Install the Microsoft.Extensions.Logging.Abstractions NuGet package to the Visual Studio project that contains custom code.
- Add the following
using
directive to the code file that contains the logic for which you need to write to the log.using Microsoft.Extensions.Logging;
- In the graph that executes the logic, add a property of the
ILogger<T> type, and assign the
InjectDependency attribute to the property, as shown in the following
example. Use the graph as the type parameter.Tip:For details about the InjectDependency attribute, see Dependency Injection.
public class RSSVWorkOrderEntry : PXGraph<RSSVWorkOrderEntry, RSSVWorkOrder> { //Other code of the graph //The logger [InjectDependency] private ILogger<RSSVWorkOrderEntry> MyLogger { get; set; } }
- In the code for which you need to write to the log, such as in an action of the graph,
call the methods of the ILogger<T> interface to write to the trace
log. For details about the methods of the interface, see ILogger<TCategoryName> Interface in the
Microsoft documentation. The following example shows the call of the
LogInformation
method.
public class RSSVWorkOrderEntry : PXGraph<RSSVWorkOrderEntry, RSSVWorkOrder> { //Other code of the graph //The logger [InjectDependency] private ILogger<RSSVWorkOrderEntry> MyLogger { get; set; } //A method in the graph public void AssignOrder(RSSVWorkOrder order) { //Implementation of the method //Writing to the log MyLogger.LogInformation( "The {OrderNbr} work order has been assigned.", order.OrderNbr); } }
Note:Do not use string formatting inside the log messages, as is shown in the following code example.MyLogger.LogInformation( string.Format("The {0} work order has been assigned.", order.OrderNbr)); MyLogger.LogInformation( $"The {order.OrderNbr} work order has been assigned."));
Writing to the Trace Log by Using the Methods of PXTrace
You can write information to the trace log directly from your code by using the following methods of the PXTrace static class:
public static void WriteError(string message)
public static void WriteError(Exception e)
public static void WriteWarning(string message)
public static void WriteWarning(Exception e)
public static void WriteInformation(string message)
public static void WriteInformation(Exception e)
public static void WriteVerbose(string message)
To manage writing information to the trace log, you can use the following methods of the PXTrace static class:
public static Serilog.ILogger WithStack()
By default, the stack trace is not collected so that the project does not impair system performance. To turn on the collection of the stack trace, call the WithStack method, as the following code example shows. Note that in the presented code, the stack trace is collected only if the appropriate logging level has been set up.PXTrace.WithStack().Information(“some info”);
public static Serilog.ILogger WithSourceLocation()
Bin
folder of MYOB Acumatica.The WithStack and WithSourceLocation methods can be chained with Serilog.ILogger methods.
Adding a Log Method to the Published Customization Code
If you want to avoid changes in the customization project, you can add this method to the customization code that has already been applied to the application instance. To do this, perform the following actions:
- Make sure that the customization project that contains the code you are debugging has been published. If the project is unpublished, publish it, as described in Publishing Customization Projects.
- Launch Microsoft Visual Studio.
- In Visual Studio, open the file with the code that is currently saved in the App_RuntimeCode website folder.
- Add a call of one of the methods listed above to the appropriate code sample.
- Save your changes to the file.
- In the browser, open or refresh the form that you are troubleshooting.
- On the form, perform the sequence of actions that invokes the changed code.
- Click MYOB Trace page. to open the
- On the page, explore the trace log to make sure that the changed code sample has been executed.
Specifying the Logging Level
Events that you log in code have different logging levels. You can specify a logging level
in web.config
so that only events up to that level will be logged. You set
up the logging level by using the following settings in the web.config
file:
system.web/pxtrace/@minimumLevel
: An indicator of the overall minimum level of severity of events that can be logged by the providers defined in thepxtrace
section.system.web/pxtrace/add/@minimumLevel
: An indicator of the minimum level of severity of events that a particular provider in thepxtrace
section must log. For example, you can specify a different level of severity for a provider that saves log messages to a file.
The logging levels are the following: Fatal
, Error
,
Warning
, Information
, Debug
,
Verbose
. By default, the Verbose
level is specified.