Error এবং Exception Management

Microsoft Technologies - এএসপি ডট নেট এমভিসি (ASP.Net MVC) রিপোর্টিং এবং লগিং (Reporting and Logging) |
177
177

ASP.Net MVC অ্যাপ্লিকেশনে Error এবং Exception Management একটি গুরুত্বপূর্ণ অংশ, কারণ এটি অ্যাপ্লিকেশনকে ক্র্যাশ হওয়া থেকে রক্ষা করে এবং ব্যবহারকারীর কাছে উপযুক্ত মেসেজ দেখাতে সাহায্য করে। এর মাধ্যমে ডেভেলপাররা অ্যাপ্লিকেশনের সমস্যাগুলো সনাক্ত এবং সমাধান করতে পারে।


Exception Management এর ধাপসমূহ

১. Try-Catch ব্লক ব্যবহার

Try-Catch ব্লক ব্যবহার করে ডেভেলপাররা কোডে ব্যতিক্রমী (exceptional) পরিস্থিতি পরিচালনা করতে পারেন।

উদাহরণ:
public IActionResult GetStudent(int id)
{
    try
    {
        var student = _context.Students.Find(id);
        if (student == null)
        {
            throw new Exception("Student not found");
        }
        return View(student);
    }
    catch (Exception ex)
    {
        // লগ বা একটি কাস্টম মেসেজ ফেরত দেওয়া
        return Content($"Error: {ex.Message}");
    }
}

২. Custom Exception তৈরি

কাস্টম Exception ক্লাস তৈরি করে নির্দিষ্ট প্রয়োজনীয়তার জন্য ব্যতিক্রম পরিচালনা করা যায়।

উদাহরণ:
public class NotFoundException : Exception
{
    public NotFoundException(string message) : base(message) { }
}

public IActionResult GetStudent(int id)
{
    try
    {
        var student = _context.Students.Find(id);
        if (student == null)
        {
            throw new NotFoundException("Student not found");
        }
        return View(student);
    }
    catch (NotFoundException ex)
    {
        return Content($"Custom Error: {ex.Message}");
    }
}

Global Exception Handling

ASP.Net MVC তে Global Exception Handling ব্যবহারের মাধ্যমে সম্পূর্ণ অ্যাপ্লিকেশনের জন্য Exception পরিচালনা করা যায়।

১. HandleError Attribute ব্যবহার

HandleError Attribute ব্যবহারের মাধ্যমে নির্দিষ্ট কন্ট্রোলার বা অ্যাকশন মেথডে ব্যতিক্রম পরিচালনা করা যায়।

উদাহরণ:
[HandleError(View = "Error")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        throw new Exception("An error occurred");
    }
}

২. Global Filter ব্যবহার

FilterConfig-এ HandleErrorAttribute অ্যাড করে অ্যাপ্লিকেশনের সমস্ত মেথডের জন্য Exception হ্যান্ডল করা যায়।

উদাহরণ:

FilterConfig.cs:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

৩. Custom Error Page ব্যবহার

Custom Error পৃষ্ঠার মাধ্যমে ব্যবহারকারীদের জন্য একটি ফ্রেন্ডলি মেসেজ দেখানো যায়।

উদাহরণ:

Web.config:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
        <error statusCode="404" redirect="~/Error/NotFound" />
    </customErrors>
</system.web>

ErrorController:

public class ErrorController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult NotFound()
    {
        return View();
    }
}

Logging ব্যবহার করে Exception Management

ডেভেলপাররা Exception লগ করতে NLog, Serilog, বা Log4Net এর মতো টুল ব্যবহার করতে পারেন।

উদাহরণ: NLog ব্যবহার

NLog.config:

<targets>
    <target name="file" xsi:type="File" fileName="Logs/logfile.log" />
</targets>
<rules>
    <logger name="*" minlevel="Error" writeTo="file" />
</rules>

Global Exception Logging:

public class GlobalExceptionHandler : IExceptionFilter
{
    public void OnException(ExceptionContext context)
    {
        var logger = LogManager.GetCurrentClassLogger();
        logger.Error(context.Exception, "An error occurred");
    }
}

Exception Management Best Practices

  • Exception স্পেসিফিক রাখুন: Always catch specific exceptions rather than catching a general Exception class.
  • Global Error Handling ব্যবহার করুন: অ্যাপ্লিকেশনের স্থায়িত্ব নিশ্চিত করতে।
  • লগিং নিশ্চিত করুন: Exception লগ করা যেন সহজ হয়, যাতে সমস্যার উৎস শনাক্ত করা যায়।
  • Custom Error Pages ব্যবহার করুন: ব্যবহারকারীদের জন্য একটি সুন্দর এবং তথ্যবহুল পৃষ্ঠা তৈরি করুন।
  • ফল্ট টলারেন্স নিশ্চিত করুন: Exception হ্যান্ডলিং-এর মাধ্যমে অ্যাপ্লিকেশন ক্র্যাশ এড়িয়ে কাজ চালিয়ে যাওয়ার ব্যবস্থা করুন।

সারমর্ম

ASP.Net MVC তে Error এবং Exception Management একটি গুরুত্বপূর্ণ দিক, যা অ্যাপ্লিকেশনকে নিরবিচ্ছিন্নভাবে চলমান রাখতে সাহায্য করে। Try-Catch ব্লক, Global Filters, Custom Error Pages, এবং Logging Tools ব্যবহার করে একটি শক্তিশালী এবং কার্যকর Exception Management সিস্টেম তৈরি করা যায়। এটি ব্যবহারকারীদের অভিজ্ঞতা উন্নত করে এবং ডেভেলপারদের জন্য ডিবাগিং সহজ করে।

common.content_added_by
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion