/ doc / devdocs / logging.md
logging.md
 1  # Logging
 2  
 3  Logging plays an important part in determining bugs in our code. It provides context for the developers about where and when errors occur.
 4  
 5  ## Where are the logs saved
 6  
 7  * Most of the logs are saved under `%LOCALAPPDATA%/Microsoft/PowerToys`.
 8  * For low-privilege processes (like preview handlers) the logs are saved under `%USERPROFILE%/AppData/LocalLow/Microsoft/PowerToys`.
 9  
10  Logs are normally in a subfolder with the module name as title.
11  
12  The [BugReportTool](/tools/BugReportTool) will take logs from both locations when executed.
13  
14  ## Using a logger in a project
15  
16  ### Spdlog
17  
18  In C++ projects we use the awesome [spdlog](https://github.com/gabime/spdlog) library for logging as a git submodule under the `deps` directory. To use it in your project, just include [spdlog.props](/deps/spdlog.props) in a .vcxproj like this:
19  
20  ```xml
21  <Import Project="..\..\..\deps\spdlog.props" />
22  ```
23  It'll add the required include dirs and link the library binary itself.
24  
25  ### PowerToys Logger in ManagedCommon
26  
27  For C# projects there is a static logger class in Managed Common called `Logger`.
28  
29  To use it, add a project reference to `ManagedCommon` and add the following line of code to all the files using the logger:
30  
31  ```Csharp
32  using ManagedCommon;
33  ```
34  
35  In the `Main` function (or a function with a similar meaning (like `App` in a `App.xaml.cs` file)) you have to call `InitializeLogger` and specify the location where the logs will be saved (always use a path scheme similar to this example):
36  
37  ```Csharp
38  Logger.InitializeLogger("\\FancyZones\\Editor\\Logs");
39  ```
40  
41  For a low-privilege process you have to set the optional second parameter to `true`:
42  
43  ```Csharp
44  Logger.InitializeLogger("\\FileExplorer\\Monaco\\Logs", true);
45  ```
46  
47  The `Logger` class contains the following logging functions:
48  
49  ```Csharp
50  // Logs an error that the utility encountered
51  Logger.LogError(string message);
52  Logger.LogError(string message, Exception ex);
53  // Logs an error that isn't that grave
54  Logger.LogWarning(string message);
55  // Logs what the app is doing at the moment
56  Logger.LogInfo(string message);
57  // Like LogInfo just with infos important for debugging
58  Logger.LogDebug(string message);
59  // Logs the current state of the utility.
60  Logger.LogTrace();
61  ```