|
| 1 | +--- |
| 2 | +title: How to generate prompts for Copilot by Copilot |
| 3 | +date: 2025-11-07 14:55:00 +0100 |
| 4 | +categories: [AI, Copilot] |
| 5 | +tags: [copilot, prompts, ai, logging, sourcegenerator] # TAG names should always be lowercase |
| 6 | +--- |
| 7 | + |
| 8 | +When I'm coding and Copilot is trying to help me, creating a good prompt can sometimes be very challenging. But did you know you can let Copilot create a prompt for you? |
| 9 | + |
| 10 | +### Context |
| 11 | +I'm replacing all ILogger messages by moving them into a separate `partial class` and making use of the source generator `ILoggerMessage` to prevent boxing when the LogLevel is not logged anyway. Nick Chapsas has a whole [Youtube Video](https://youtu.be/a26zu-pyEyg?t=676) explaining this topic in detail, but the 'why' is out-of-scope for this blog. |
| 12 | + |
| 13 | +This refactoring work is repetitive and tedious, so how can Copilot help me with this? I had no clue what kind of prompt I should give it. So how about asking Copilot for help? |
| 14 | + |
| 15 | +### Getting Copilot to Create a Prompt |
| 16 | + |
| 17 | +I created two files: the original version and a second one with the changes applied. Then I simply asked Copilot for a prompt: |
| 18 | + |
| 19 | +``` |
| 20 | +I have these two C# files showing a before and after refactoring where I moved all ILogger calls into a separate static partial class using the LoggerMessage source generator pattern. Please analyze the changes and create a clear, copy-and-paste prompt that I can use with Copilot to apply this same refactoring pattern to other classes in the future. The prompt should specify the ILoggerMessage approach and include concrete examples. |
| 21 | +``` |
| 22 | + |
| 23 | +Copilot will now return a prompt that I can use later in other classes. |
| 24 | + |
| 25 | +``` |
| 26 | +Refactor this class to move all logging statements into a separate static partial class using the LoggerMessage attribute pattern. |
| 27 | +Instructions: |
| 28 | +• Create a static partial class named [ClassName]Log (replace [ClassName] with the main class name). |
| 29 | +• Add the newly created class into the same file as [ClassName] |
| 30 | +• For each log statement, define a corresponding static partial method in the log class, decorated with [LoggerMessage] and the appropriate log level, message, and parameters. |
| 31 | +• Replace direct calls to _logger.LogInformation, _logger.LogError, etc., in the main class with calls to the new static partial methods. |
| 32 | +• Ensure the main class only calls these new logging methods, passing the logger instance as this ILogger<[ClassName]> logger. |
| 33 | +• Do not change the logic or structure of the main class, except for replacing the logging calls. |
| 34 | +Example: |
| 35 | +// Before: _logger.LogInformation("Starting scheduled {HttpMethod} call to {RequestPath}", method, url); |
| 36 | +// After: _logger.StartingScheduledHttpCall(method, url); |
| 37 | +// In the static partial log class: [LoggerMessage(Level = LogLevel.Information, Message = "Starting scheduled {HttpMethod} call to {RequestPath}")] public static partial void StartingScheduledHttpCall(this ILogger<MitzHttpJob> logger, HttpMethod httpMethod, string? requestPath); |
| 38 | +``` |
0 commit comments