18/09/2021

Sử dụng log4net ghi log trong ASP.NET MVC

 Khi bạn cần debug hay handle exception trong chương trình

bạn có thể tự viết hàm ghi log nhưng log4net có thể làm giúp bạn rất tốt với 5 kiểu khác nhau:

  • Debug
  • Information
  • Warnings
  • Error
  • Fatal
Dưới dây là 1 đoạn log exception:

2021-09-18 15:30:43,751 [5] ERROR MonitoringLogger - Load index error
System.DivideByZeroException: Attempted to divide by zero.
at WebApp.Controllers.HomeController.Index() in P:\****\Controllers\HomeController.cs:line 37

Tham khảo bài viết chi tiết tại đây: https://vnn.me/sJlMw9v

12/08/2021

Export table html to excel with utf-8 using javascript

 // đây là hàm export toàn bộ nội dung trong 1 bảng html ra excel

function exportTableToExcel(tableID, filename = '') {

        var downloadLink;

        var dataType = 'application/vnd.ms-excel;charset=utf-8';

        var tableSelect = document.getElementById(tableID);

        var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');


        // Specify file name

        filename = filename ? filename + '.xls' : 'excel_data.xls';


        // Create download link element

        downloadLink = document.createElement("a");


        document.body.appendChild(downloadLink);


        if (navigator.msSaveOrOpenBlob) {

            var blob = new Blob(['\ufeff', tableHTML], {

                type: dataType

            });

            navigator.msSaveOrOpenBlob(blob, filename);

        } else {

            // Create a link to the file

            downloadLink.href = 'data:' + dataType + ', ' + tableHTML;


            // Setting the file name

            downloadLink.download = filename;


            //triggering the function

            downloadLink.click();

        }

    }


//sử dụng

<button class="btn btn-success" onclick="exportTableToExcel('tableId', 'file_excel.xlsx')">Export</button>

11/04/2021

Mã hóa số thành chuỗi với Base62 c#

 Đôi khi bạn cần mã hóa 1 số thành chuỗi, để đảm bảo chuỗi mã hóa không trùng, bạn cần 1 số long và kiểu mã hóa đủ rộng, ở đây có mô tả Base62

code sample như sau:

private string ToBase62(long number)

        {

            try

            {

                var alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

                var n = number;

                long basis = 62;

                var ret = "";

                while (n > 0)

                {

                    long temp = n % basis;

                    ret = alphabet[(int)temp] + ret;

                    n = (n / basis);

                }

                return ret;

            }

            catch(Exception ex)

            {

                //Helpers.Log.Error("ToBase62 error" + number.ToString(), ex); //hander exception error

                return string.Empty;

            }

        }

10/03/2021

Kiểm tra 1 trang web có cho phép mở trong iframe hay không bằng c#

 Khi cần nhúng 1 trang web (URL) khác vào website của bạn dưới dạng iframe

Nếu website đó không cho phép mở dưới dạng iframe, website bạn sẽ hiển thị lỗi :)

Bạn cần kiểm tra trước để hiển thị phù hợp, code c# có thể dùng như sau:

//using using System.Net;

//check frame support

                    string frame_support;

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(iframeUrl);

                    using (WebResponse response = request.GetResponse())

                    {

                        //DENY, SAMEORIGIN, ALLOW-FROM

                        //null is open in iframe

                        frame_support = response.Headers["X-Frame-Options"];

                    }

25/02/2021

Dạo phố Việt Trì 25 Tết Tân Sửu


Đi dạo phố với dream II Thái tèn tèn chậm chậm 5km/1h :)

————
Nguyễn Minh Tuấn
skype: tuan.net
mobile: 038 68 68 247

Đăng ký tên miền, hosting, máy chủ, thiết kế lập trình website theo yêu cầu

 Chính thức trở thành đơn vị cung cấp dịch vụ đăng ký tên miền quốc tế, tên miền Việt Nam hosting, máy chủ, cloud hosting, cloud server, ema...