In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use Func delegation to achieve API log records, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
problem
Usually when we develop web api, we generally need to record the input and output information of api to facilitate follow-up troubleshooting; then what we usually do is we write a public method in a public place to control input and output. At this time, Func
What is Func?
Func encapsulates a method that takes one parameter and returns the type value specified by the TResult parameter. Use a Func delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have a parameter passed to it by value, and must return a value. When using a Func delegate, it is not necessary to explicitly define a delegate that encapsulates a method with only one parameter.
Main code implementation
1. Define delegation.
Private ApiResult HandleWork (CreateBQoolSyncEventModel syncModel, Func work) {/ / first register to Db Event _ innerBQoolSyncEventLogService.Create (syncModel); var result = new ApiResult (); try {_ logger.Info ($"Call Web Api Start, data: {syncModel.ToJsonString ()}"); result = work () If (! result.Success & &! string.IsNullOrEmpty (result.Code) & & string.IsNullOrEmpty (result.ErrorMessage)) {result.ErrorMessage = ApiResultCode.ErrorMessages [result.Code];}} catch (Exception ex) {_ logger.Error (ex.ToString ()) Result.Success = false; if (string.IsNullOrEmpty (result.ErrorMessage)) {result.ErrorMessage = ex.Message;} else {result.ErrorMessage + = ";" + ex.Message }} finally {_ logger.Info ($"Call Web Api End, result: {result.ToJsonString ()}"); / / Update Db event syncModel.SetStatus (result.Success? BQoolSyncEventStatus.Success: BQoolSyncEventStatus.Failure); _ innerBQoolSyncEventLogService.ChangeStatus (syncModel.Id, syncModel.Status, result.ErrorMessage);} return result;}
2. Take the method as a parameter and pass in the delegate.
[HttpPost] public ApiResult ModuleFeatureSetting (ReviewsApiRequestModel model) {string data = StringTools.AESDecrypt (model.Encrypt); var param = JObject.Parse (data). ToObject (); ApiResult result = HandleWork (new CreateBQoolSyncEventModel (param.Account, BQoolSyncEventType.ModuleFeatureSetting, param), () = > _ ReviewsSettingSyncService.ModuleFeatureSetting (param)); return result;}
3. Check the log.
Thank you for reading this article carefully. I hope the article "how to use Func delegation to record API logs" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.