PDA

View Full Version : چه طور می توان Exception های رخ داده را به ایمیل سازنده فرستاد؟



NasimBamdad
چهارشنبه 21 آذر 1397, 23:11 عصر
سلام و وقت بخیر

سوالی پیش آمد که برای بهتر شدن نرم افزار ها بعد از رخ دادن Exception ، متن خطا رو ایمیل کرد . آیا راهی هست >؟

سناریو اول این هست که همه جا از Try Catach استفاده کرد و در زمان catch کد مربوط به ایمیل رو ارسال کرد.

آیا سناریو دیگه ای هست ؟ مثلا نرم افزار های مبهم ساز این قابللیت به نرم افزار اضافه می کنند . چه طور این مورد انجام میشه ؟

رامین مرادی
پنج شنبه 22 آذر 1397, 09:04 صبح
منم این مورد رو نیاز دارم.
البته یه مشکلی هم پیش میاد که موقع ارسال اگه دسترسی به اینترنت نباشه یا سرور میل در دسترس نباشه یه اکسپشن دیگه ایجاد میشه.

بجای تکرار کدها یه کلاس بسازیم که ورودی سازندش کلاسی از نوع اکسپشن باشه. تو کچ ها این کلاس رو فراخوانی کنیم و اکسپشن ایجاد شده رو به ورودی کلاس بدیم. حالا تو داخل کلاس قبل از ارسال خطاها رو تو یه سطر از فایل متنی نگه داری کنیم. و به محض دسترسی به سرور اطلاعات رو ارسال کنیم. به نظر من از هاست استفاده کنید و بتونید یه برنامه مدیریت خطا هم بنویسید برا خودتون. بعد ارسال خطاهای قبلی رو پاک کنید.

البته امیدوارم دوستان نظرات خوبی بدن چون من خودمم به این مورد نیاز دارم و به راه حل جامعی نرسیدم.

Mahmoud Zaad
پنج شنبه 22 آذر 1397, 09:48 صبح
با سلام
برای قسمت اول:
static void Main()
{
Application.ThreadException +=
new ThreadExceptionEventHandler(Application_ThreadExce ption);

// Your designer generated commands.
}


static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{

var fromAddress = new MailAddress("your Gmail address", "Your name");
var toAddress = new MailAddress("email address where you want to receive reports", "Your name");
const string fromPassword = "your password";
const string subject = "exception report";
Exception exception = e.Exception;
string body = exception.Message + "\n" + exception.Data + "\n" + exception.StackTrace + "\n" + exception.Source;

var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
//You can also use SendAsync method instead of Send so your application begin invoking instead of waiting for send mail to complete. SendAsync(MailMessage, Object) :- Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.
smtp.Send(message);
}
}

حالا به جای ایمیل، هر کار دیگه ای میشه انجام داد.
منبع (https://stackoverflow.com/questions/1906006/error-reporting-framework-for-net)

shahryari
پنج شنبه 22 آذر 1397, 10:16 صبح
سلام
ارسال از طریق ایمیل صحیح نیست، گاهی پورتش عوض میشه گاهی stmp موقتا غیرفعال میشه و نیاز تایید و فعالسازی دوباره داره و ...... (تجربه ای که خودم دارم تابحال ندیدم ارسال ایمیل با smtp درست درمون کارکنه ینی بگیر نگیر داره!!) درثانی امنیت هم نداره که بخوای یوزر و پسورد ایملت رو بزاری در برنامه
یه وب سرویس بنویس و متن خطا رو در دیتابیس سرور ذخیره کنید کاری که خود مایکروسافت انجام میده
https://sc.cnbcfm.com/applications/cnbc.com/resources/files/2014/02/12/412567-windows-error-reporting.jpg


We have solved this by creating a simple WCF web service that takes the stack trace, zipped logs etc, which the client posts to it and then let the web service server send the email. This way you don't have to have the SMTP authentication information on the client side, nor do you have to rely on the user to submit the error report via their email client.

We're now even zipping and submitting client side logs periodically to the backend server via this webservice and analyze those logs to preemptively detect any client side issues before they become problematic to the client

این کتابخونه هم واسه همین نوشته شده ، البته نحوه کارکردشو اطلاع ندارم
https://archive.codeplex.com/?p=crashreporterdotnet